-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path18.cpp
245 lines (218 loc) · 5.95 KB
/
18.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <array>
#include <chrono>
#include <iostream>
#include <vector>
using std::stoi;
using std::string;
using std::vector;
enum class Op {
SND,
SET,
ADD,
MUL,
MOD,
RCV,
JGZ,
};
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("snd")) {
i.op = Op::SND;
} else if (line.starts_with("set")) {
i.op = Op::SET;
} else if (line.starts_with("add")) {
i.op = Op::ADD;
} else if (line.starts_with("mul")) {
i.op = Op::MUL;
} else if (line.starts_with("mod")) {
i.op = Op::MOD;
} else if (line.starts_with("rcv")) {
i.op = Op::RCV;
} else if (line.starts_with("jgz")) {
i.op = Op::JGZ;
} else {
throw std::runtime_error(std::string("invalid 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;
}
int64_t solve_pt1(const vector<Instruction> &ins) {
std::array<int64_t, 26> registers = {0};
int64_t sound = 0;
// 191 too low
// 316 too low
for (size_t i = 0; i < ins.size();) {
const Instruction &in = ins[i];
switch (in.op) {
case Op::SND: {
sound = registers[in.a.value];
break;
}
case Op::SET: {
registers[in.a.value] =
in.b.type == REGISTER_ADDRESS ? registers[in.b.value] : in.b.value;
break;
}
case Op::ADD: {
registers[in.a.value] +=
in.b.type == REGISTER_ADDRESS ? registers[in.b.value] : in.b.value;
break;
}
case Op::MUL: {
registers[in.a.value] *=
in.b.type == REGISTER_ADDRESS ? registers[in.b.value] : in.b.value;
break;
}
case Op::MOD: {
registers[in.a.value] %=
in.b.type == REGISTER_ADDRESS ? registers[in.b.value] : in.b.value;
break;
}
case Op::RCV: {
if (registers[in.a.value] != 0) {
return sound;
}
break;
}
case Op::JGZ: {
int64_t value =
in.a.type == REGISTER_ADDRESS ? registers[in.a.value] : in.a.value;
if (value > 0) {
i += (in.b.type == REGISTER_ADDRESS ? registers[in.b.value]
: in.b.value);
continue;
}
break;
}
}
i++;
}
return 0;
}
using std::array;
int64_t solve_pt2(const vector<Instruction> &ins) {
array<array<int64_t, 26>, 2> registers = {
array<int64_t, 26>{0},
array<int64_t, 26>{0},
};
registers[0]['p' - 'a'] = 0;
registers[1]['p' - 'a'] = 1;
array<vector<int64_t>, 2> tx;
array<size_t, 2> txp = {0, 0};
array<size_t, 2> ip = {0, 0};
unsigned int active_program = 0;
bool deadlock = false;
// alternate between programs until a recv instruction
while (ip[active_program] < ins.size()) {
const Instruction &in = ins[ip[active_program]];
switch (in.op) {
case Op::SND: {
tx[active_program].push_back(registers[active_program][in.a.value]);
break;
}
case Op::SET: {
registers[active_program][in.a.value] =
in.b.type == REGISTER_ADDRESS ? registers[active_program][in.b.value]
: in.b.value;
break;
}
case Op::ADD: {
registers[active_program][in.a.value] +=
in.b.type == REGISTER_ADDRESS ? registers[active_program][in.b.value]
: in.b.value;
break;
}
case Op::MUL: {
registers[active_program][in.a.value] *=
in.b.type == REGISTER_ADDRESS ? registers[active_program][in.b.value]
: in.b.value;
break;
}
case Op::MOD: {
registers[active_program][in.a.value] %=
in.b.type == REGISTER_ADDRESS ? registers[active_program][in.b.value]
: in.b.value;
break;
}
case Op::RCV: {
if (tx[active_program == 0 ? 1 : 0].size() < 1 + txp[active_program]) {
active_program = active_program == 0 ? 1 : 0;
if (deadlock) {
return tx[1].size();
} else {
deadlock = true;
continue;
}
} else {
deadlock = false;
registers[active_program][in.a.value] =
tx[active_program == 0 ? 1 : 0][txp[active_program]];
txp[active_program]++;
}
break;
}
case Op::JGZ: {
int64_t value = in.a.type == REGISTER_ADDRESS
? registers[active_program][in.a.value]
: in.a.value;
if (value > 0) {
ip[active_program] += (in.b.type == REGISTER_ADDRESS
? registers[active_program][in.b.value]
: in.b.value);
continue;
}
break;
}
}
ip[active_program]++;
}
// if both programs are at a recv instruction
// return length of tx[1]
return tx[1].size();
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
std::string line;
std::vector<Instruction> ins = parse_input();
int64_t pt1 = solve_pt1(ins);
int64_t pt2 = solve_pt2(ins);
std::cout << "--- Day 18: Duet ---\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;
}