-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_21b.cpp
305 lines (277 loc) · 8.16 KB
/
day_21b.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
#include <queue>
#include <stack>
#include <chrono>
#include <thread>
constexpr long long pow(const int base, const int raised_to) {
long long ans = 1;
for (int i = 1; i <= raised_to; i++) {
ans *= base;
}
return ans;
}
struct hash_pair {
template <class T1, class T2>
size_t operator()(const std::pair<T1, T2>& p) const {
auto hash1 = std::hash<T1>{}(p.first);
auto hash2 = std::hash<T2>{}(p.second);
return hash1 ^ hash2;
}
};
class Program {
public:
Program(const std::vector<long long>& program) : init_program_state(program) {
for (long long i = 0; i < program.size(); i++) {
memory[i] = program[i];
}
}
void InitIfNotInited(const long long key) {
if (memory.find(key) == memory.end()) {
memory[key] = 0;
}
}
void reset() {
for (long long i = 0; i < init_program_state.size(); i++) {
memory[i] = init_program_state[i];
}
complete = false;
inst_ptr = 0;
relative_base = 0;
}
std::vector<long long> getNOps(const size_t n, const size_t inst_ptr,
long long modes) {
std::vector<long long> ops;
for (size_t i = 1; i <= n; ++i) {
size_t rem = modes % 10;
modes = modes / 10;
if (rem == 0) {
InitIfNotInited(inst_ptr + i);
InitIfNotInited(memory[inst_ptr + i]);
ops.push_back(memory[memory[inst_ptr + i]]);
} else if (rem == 1) {
InitIfNotInited(inst_ptr + i);
InitIfNotInited(memory[inst_ptr + i]);
ops.push_back(memory[inst_ptr + i]);
} else if (rem == 2) {
InitIfNotInited(inst_ptr + i);
InitIfNotInited(relative_base + memory[inst_ptr + i]);
InitIfNotInited(memory[relative_base + memory[inst_ptr + i]]);
ops.push_back(memory[relative_base + memory[inst_ptr + i]]);
} else {
// // std::cout << "Error" << '\n';
exit(0);
}
}
return ops;
}
long long getAddress(const size_t increment, long long modes) {
long long address;
const size_t rem = modes;
if (rem == 0) {
InitIfNotInited(inst_ptr + increment);
address = memory[inst_ptr + increment];
} else if (rem == 2) {
InitIfNotInited(inst_ptr + increment);
InitIfNotInited(relative_base + memory[inst_ptr + increment]);
address = relative_base + memory[inst_ptr + increment];
} else {
// // std::cout << "Error" << '\n';
exit(0);
}
return address;
}
long long compute(const std::vector<long long>& input) {
is_input_requested = false;
long long output = -1;
size_t input_ptr = 0;
while (memory[inst_ptr] != 99) {
// std::cout << inst_ptr << '\n';
const long long instr = memory[inst_ptr];
const long long opcode = instr % 100;
const long long modes = instr / 100;
if (opcode == 1) {
constexpr int n_ops = 2;
const auto ops = getNOps(n_ops, inst_ptr, modes);
memory[getAddress(3, modes / pow(10, n_ops))] = ops[0] + ops[1];
inst_ptr += 4;
} else if (opcode == 2) {
constexpr int n_ops = 2;
const auto ops = getNOps(n_ops, inst_ptr, modes);
memory[getAddress(3, modes / pow(10, n_ops))] = ops[0] * ops[1];
inst_ptr += 4;
} else if (opcode == 3) {
if (input.size() == input_ptr) {
input_ptr = 0;
is_input_requested = true;
return -1;
}
memory[getAddress(1, modes)] = input[input_ptr];
inst_ptr += 2;
input_ptr += 1;
} else if (opcode == 4) {
const auto ops = getNOps(1, inst_ptr, modes);
output = ops[0];
inst_ptr += 2;
return output;
} else if (opcode == 5) {
const auto ops = getNOps(2, inst_ptr, modes);
if (ops[0] != 0) {
inst_ptr = ops[1];
} else {
inst_ptr += 3;
}
} else if (opcode == 6) {
const auto ops = getNOps(2, inst_ptr, modes);
if (ops[0] == 0) {
inst_ptr = ops[1];
} else {
inst_ptr += 3;
}
} else if (opcode == 7) {
constexpr int n_ops = 2;
const auto ops = getNOps(n_ops, inst_ptr, modes);
if (ops[0] < ops[1]) {
memory[getAddress(3, modes / pow(10, n_ops))] = 1;
} else {
memory[getAddress(3, modes / pow(10, n_ops))] = 0;
}
inst_ptr += 4;
} else if (opcode == 8) {
constexpr int n_ops = 2;
const auto ops = getNOps(n_ops, inst_ptr, modes);
if (ops[0] == ops[1]) {
memory[getAddress(3, modes / pow(10, n_ops))] = 1;
} else {
memory[getAddress(3, modes / pow(10, n_ops))] = 0;
}
inst_ptr += 4;
} else if (opcode == 9) {
const auto ops = getNOps(1, inst_ptr, modes);
relative_base += ops[0];
inst_ptr += 2;
}
}
complete = true;
return output;
}
bool isComplete() { return complete; }
bool isProgramWaitingForInput() {
return is_input_requested;
}
void modifyMemoryAddress(const long long address, const long long value) {
memory[address] = value;
}
private:
const std::vector<long long> init_program_state;
std::unordered_map<long long, long long> memory;
bool complete = false;
size_t inst_ptr = 0;
size_t relative_base = 0;
bool is_input_requested = false;
};
class IntcodeComputer {
public:
void addProgram(const std::vector<long long>& program) {
programs.emplace_back(program);
}
long long runProgram(const size_t index,
const std::vector<long long>& input) {
return programs[index].compute(input);
}
void resetProgram(const size_t index) { programs[index].reset(); }
void modifyProgramMemoryAddress(const size_t index, const long long address, const long long value) {
programs[index].modifyMemoryAddress(address, value);
}
bool isLastComplete() { return programs.back().isComplete(); }
bool isComplete(size_t index) { return programs[index].isComplete(); }
void reset() { programs.clear(); }
bool isProgramWaitingForInput(const size_t index) {
return programs[index].isProgramWaitingForInput();
}
private:
std::vector<Program> programs;
};
void PrintGrid(const std::vector<std::vector<char>>& view) {
for(const auto & row : view) {
for (const auto c : row) {
std::cout << c;
}
std::cout << '\n';
}
std::cout << '\n';
}
std::vector<long long> convertToAscii(const std::string& str) {
std::vector<long long> ret;
for(const char c: str) {
ret.push_back(static_cast<long long>(c));
}
return ret;
}
int main(int argc, char* argv[]) {
// Get input
std::string input = "../input/day_21_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string input_str;
std::getline(file, input_str);
const std::string delimiter = ",";
std::size_t start = 0;
std::size_t end = input_str.find(delimiter);
std::vector<long long> program;
while (end != std::string::npos) {
program.emplace_back(std::stoll(input_str.substr(start, end - start)));
start = end + delimiter.size();
end = input_str.find(delimiter, start);
}
program.emplace_back(std::stoll(input_str.substr(start, end - start)));
// Solve
std::vector<std::string> instructions {
"NOT A T\n",
"OR T J\n",
"NOT B T\n",
"OR T J\n",
"NOT C T\n",
"OR T J\n",
"AND D J\n",
"NOT H T\n",
"NOT T T\n",
"OR E T\n",
"AND T J\n",
"RUN\n"
};
// Another Option:
// "NOT C J\n",
// "AND D J\n",
// "AND H J\n",
// "NOT B T\n",
// "AND D T\n",
// "OR T J",
// "NOT A T\n",
// "OR T J\n",
// "RUN\n"
IntcodeComputer computer;
computer.addProgram(program);
computer.runProgram(0, {});
long long output = 0;
for (int i = 0; i < instructions.size(); i++) {
while (!computer.isProgramWaitingForInput(0)) {
computer.runProgram(0, {});
}
output = computer.runProgram(0, convertToAscii(instructions[i]));
}
while (!computer.isComplete(0)) {
output = computer.runProgram(0, {});
if (output > 127) { // Ascii range for signed
break;
}
}
std::cout << output << '\n';
return output;
}