-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_10a.cpp
61 lines (48 loc) · 1.04 KB
/
day_10a.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <fstream>
#include <iostream>
#include <string>
class CommSystem {
public:
void noop() {
tick();
};
void addx(const int val) {
tick();
tick();
X += val;
}
long long get_signal_strength() {
return signal_strength;
}
private:
void tick() {
cycle_count++;
if ((cycle_count - 20) % 40 == 0) {
signal_strength += cycle_count * X;
}
}
long long X = 1;
long long cycle_count = 0;
long long signal_strength = 0;
};
int main(int argc, char * argv[]) {
std::string input = "../input/day_10_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
CommSystem cs;
while(std::getline(file, line)) {
// std::cout << line << '\n';
const auto space = line.find(' ');
const auto instr = line.substr(0, space);
if (instr == "noop") {
cs.noop();
} else if (instr == "addx") {
cs.addx(std::stoi(line.substr(space + 1, line.size() - space - 1)));
}
}
std::cout << cs.get_signal_strength() << '\n';
return 0;
}