blob: 4124fc736b3d2ca360931fb4908a6793a8e2100d (
plain)
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
37
38
39
40
41
42
43
44
45
46
|
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class VM
{
int x = 1;
int cycles = 0;
void delay(int amt) {
for (int i = 0; i < amt; i++) {
cycles++;
if (cycles % 40 == 20) {
part1 += cycles * x;
}
}
}
public:
int part1 = 0;
void parse(string line) {
istringstream stream(line);
string cmd;
stream >> cmd;
if (cmd == "noop") {
delay(1);
} else if (cmd == "addx") {
int d;
stream >> d;
delay(2);
x += d;
}
}
};
int
main()
{
VM vm;
for (string line; getline(cin, line); ) {
vm.parse(line);
}
cout << vm.part1 << endl;
}
|