-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
153 lines (122 loc) · 4.34 KB
/
solution.cpp
File metadata and controls
153 lines (122 loc) · 4.34 KB
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
#include <utility>
#include <vector>
#include <sstream>
#include <iostream>
#include <cmath>
using namespace std;
class Intcode {
vector<int> initialMemory;
vector<int> workingMemory;
public:
explicit Intcode(vector<int> memory) {
initialMemory = move(memory);
}
void init() {
workingMemory = initialMemory;
}
void run(int userID) {
int ip = 0x00;
int opcode, instruction, paramA, paramB, paramC;
while (0x00 <= ip && ip < workingMemory.size()) {
opcode = workingMemory[ip];
instruction = opcode % 100;
switch (instruction) {
case 1:
paramA = getParameter(workingMemory, ip, 1);
paramB = getParameter(workingMemory, ip, 2);
paramC = getParameter(workingMemory, ip, 3);
workingMemory[paramC] = workingMemory[paramA] + workingMemory[paramB];
ip = ip + 4;
continue;
case 2:
paramA = getParameter(workingMemory, ip, 1);
paramB = getParameter(workingMemory, ip, 2);
paramC = getParameter(workingMemory, ip, 3);
workingMemory[paramC] = workingMemory[paramA] * workingMemory[paramB];
ip = ip + 4;
continue;
case 3:
paramA = getParameter(workingMemory, ip, 1);
workingMemory[paramA] = readInput(userID);
ip = ip + 2;
continue;
case 4:
paramA = getParameter(workingMemory, ip, 1);
if (workingMemory[paramA] != 0) {
writeOutput(workingMemory[paramA]);
}
ip = ip + 2;
continue;
case 5:
paramA = getParameter(workingMemory, ip, 1);
paramB = getParameter(workingMemory, ip, 2);
ip = workingMemory[paramA] != 0 ? workingMemory[paramB] : ip + 3;
continue;
case 6:
paramA = getParameter(workingMemory, ip, 1);
paramB = getParameter(workingMemory, ip, 2);
ip = workingMemory[paramA] == 0 ? workingMemory[paramB] : ip + 3;
continue;
case 7:
paramA = getParameter(workingMemory, ip, 1);
paramB = getParameter(workingMemory, ip, 2);
paramC = getParameter(workingMemory, ip, 3);
workingMemory[paramC] = workingMemory[paramA] < workingMemory[paramB] ? 1 : 0;
ip = ip + 4;
continue;
case 8:
paramA = getParameter(workingMemory, ip, 1);
paramB = getParameter(workingMemory, ip, 2);
paramC = getParameter(workingMemory, ip, 3);
workingMemory[paramC] = workingMemory[paramA] == workingMemory[paramB] ? 1 : 0;
ip = ip + 4;
continue;
case 99:
return;
default:
throw logic_error("Unknown operation");
}
}
throw logic_error("Invalid memory address");
}
private:
static int getMode(int opcode, int offset) {
int base = pow(10, offset + 1);
return (opcode / base) % 10;
}
static int getParameter(vector<int> &memory, int ip, int offset) {
switch (getMode(memory[ip], offset)) {
case 0:
return memory[ip + offset];
case 1:
return ip + offset;
default:
throw logic_error("Unknown parameter mode");
}
}
static int readInput(int userID) {
return userID;
}
static void writeOutput(int output) {
cout << output << endl;
}
};
vector<int> getInput() {
vector<int> memory;
string line;
getline(cin, line);
stringstream ss(line);
string number;
while (getline(ss, number, ',')) {
memory.push_back(stoi(number));
}
return memory;
}
int main() {
Intcode intcode(getInput());
intcode.init();
intcode.run(1);
intcode.init();
intcode.run(5);
return 0;
}