-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_09a.cpp
200 lines (182 loc) · 5.67 KB
/
day_09a.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
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) {
long long output = -1;
size_t input_ptr = 0;
while (memory[inst_ptr] != 99) {
const long long instr = memory[inst_ptr];
const long long opcode = instr % 100;
const long long modes = instr / 100;
if (opcode == 1) {
const auto ops = getNOps(2, inst_ptr, modes);
memory[getAddress(3, modes / 100)] = ops[0] + ops[1];
inst_ptr += 4;
} else if (opcode == 2) {
const auto ops = getNOps(2, inst_ptr, modes);
memory[getAddress(3, modes / 100)] = ops[0] * ops[1];
inst_ptr += 4;
} else if (opcode == 3) {
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;
std::cout << output << '\n';
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) {
const auto ops = getNOps(2, inst_ptr, modes);
if (ops[0] < ops[1]) {
memory[getAddress(3, modes / 100)] = 1;
} else {
memory[getAddress(3, modes / 100)] = 0;
}
inst_ptr += 4;
} else if (opcode == 8) {
const auto ops = getNOps(2, inst_ptr, modes);
if (ops[0] == ops[1]) {
memory[getAddress(3, modes / 100)] = 1;
} else {
memory[getAddress(3, modes / 100)] = 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; }
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;
};
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(); }
bool isLastComplete() { return programs.back().isComplete(); }
bool isComplete(size_t index) { return programs[index].isComplete(); }
void reset() { programs.clear(); }
private:
std::vector<Program> programs;
};
int main(int argc, char* argv[]) {
// Get input
std::string input = "../input/day_09_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
constexpr int input_code = 1;
IntcodeComputer computer;
computer.addProgram(program);
while (!computer.isComplete(0)) {
computer.runProgram(0, {input_code});
}
}