-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path23.cpp
160 lines (135 loc) · 3.2 KB
/
23.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <array>
#include <chrono>
#include <cstdint>
#include <iostream>
#include <vector>
using std::stoi;
using std::string;
using std::vector;
enum class Op {
SET,
SUB,
MUL,
JNZ,
};
enum param_type { REGISTER_ADDRESS, VALUE };
struct Param {
param_type type;
int value;
};
struct Instruction {
Op op;
Param a;
Param b;
};
vector<Instruction> parse_input() {
string line;
vector<Instruction> ins;
while (std::getline(std::cin, line)) {
Instruction i;
// parse op
if (line.starts_with("set")) {
i.op = Op::SET;
} else if (line.starts_with("sub")) {
i.op = Op::SUB;
} else if (line.starts_with("mul")) {
i.op = Op::MUL;
} else if (line.starts_with("jnz")) {
i.op = Op::JNZ;
} else {
throw std::runtime_error(std::string("invalid line: " + line));
}
// parse first parameter
if (isalpha(line.at(4))) {
i.a = {REGISTER_ADDRESS, line.at(4) - 'a'};
} else {
i.a = {VALUE, stoi(&line[4])};
}
// parse 2nd parameter
if (line.length() >= 6) {
if (isalpha(line.at(6))) {
i.b = {REGISTER_ADDRESS, line.at(6) - 'a'};
} else {
i.b = {VALUE, stoi(&line[6])};
}
}
ins.push_back(i);
}
return ins;
}
int solve_pt1(const vector<Instruction> ins) {
int mul_count = 0;
std::array<int, 26> registers = {0};
for (size_t ip = 0; ip < ins.size(); ip++) {
const Instruction &in = ins[ip];
switch (in.op) {
case Op::SET: {
registers[in.a.value] =
in.b.type == REGISTER_ADDRESS ? registers[in.b.value] : in.b.value;
break;
};
case Op::SUB: {
registers[in.a.value] -=
in.b.type == REGISTER_ADDRESS ? registers[in.b.value] : in.b.value;
break;
};
case Op::MUL: {
mul_count++;
registers[in.a.value] *=
in.b.type == REGISTER_ADDRESS ? registers[in.b.value] : in.b.value;
break;
};
case Op::JNZ:
int value =
in.a.type == REGISTER_ADDRESS ? registers[in.a.value] : in.a.value;
int jump_offset =
in.b.type == REGISTER_ADDRESS ? registers[in.b.value] : in.b.value;
if (value != 0) {
ip += jump_offset - 1;
continue;
}
break;
}
}
return mul_count;
}
// part 2: 1001 too high
int solve_pt2() {
int b = 79 * 100 + 100000;
int c = b + 17000;
int d = 0, f = 0, g = 0, h = 0;
do {
f = 1;
d = 2;
for (; d * d < b; ++d) {
if (b % d == 0) {
f = 0;
break;
}
}
if (f == 0) {
h++;
}
g = b - c;
b += 17;
} while (g != 0);
return h;
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
int pt1 = 0;
int pt2 = 0;
vector<Instruction> ins = parse_input();
pt1 = solve_pt1(ins);
pt2 = solve_pt2();
std::cout << "--- Day 23: Coprocessor Conflagration ---\n";
std::cout << "Part 1: " << pt1 << "\n";
std::cout << "Part 2: " << pt2 << "\n";
auto tstop = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(tstop - tstart);
std::cout << "Time: " << (static_cast<double>(duration.count()) / 1000.0)
<< " ms"
<< "\n";
return EXIT_SUCCESS;
}