-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_08a.cpp
36 lines (33 loc) · 899 Bytes
/
day_08a.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <string_view>
#include <vector>
int main() {
std::ifstream file{"../input/day_08_input"};
std::vector<std::string> code_lines;
std::string line;
while (std::getline(file, line)) {
code_lines.emplace_back(line);
}
std::vector<bool> executed(code_lines.size(), false);
int line_n = 0;
int acc = 0;
while (true) {
if (executed[line_n]) break;
const std::string_view inst = code_lines[line_n].substr(0, 3);
executed[line_n] = true;
if (inst == "nop") {
++line_n;
} else if (inst == "acc") {
acc += stoi(code_lines[line_n].substr(4, code_lines[line_n].size() - 4));
++line_n;
} else if (inst == "jmp") {
line_n +=
stoi(code_lines[line_n].substr(4, code_lines[line_n].size() - 4));
}
}
std::cout << acc << '\n';
return acc;
}