-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.cpp
90 lines (77 loc) · 1.65 KB
/
day9.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
/*
* day9.cpp
*
* Created on: Dec 9, 2019
* Author: sanja
*/
#include <iostream>
#include <vector>
#include <unordered_map>
long long int parse(long long int input, std::vector<long long int>& opcode) {
long long int i = 0, relative_base = 0;
while (opcode[i] != 99) {
int opc = opcode[i] % 100;
int mode_1 = (opcode[i] / 100) % 10;
int mode_2 = (opcode[i] / 1000) % 10;
int mode_3 = (opcode[i] / 10000) % 10;
long long int a, b, c;
if(mode_1 == 2) a = (long long int) opcode[relative_base + opcode[i + 1]];
else a = mode_1 == 1 ? opcode[i + 1] : opcode[opcode[i + 1]];
if (mode_2 == 2) b = (long long int) opcode[relative_base + opcode[i + 2]];
else b = mode_2 == 1 ? opcode[i + 2] : opcode[opcode[i + 2]];
if (mode_3 == 2) c = relative_base + opcode[i + 3];
else c = opcode[i + 3];
switch (opc) {
case 1:
opcode[c] = a + b;
i += 4;
break;
case 2:
opcode[c] = a * b;
i += 4;
break;
case 3:
if(mode_1 == 2) opcode[relative_base + opcode[i + 1]] = input;
else opcode[opcode[i + 1]] = input;
i += 2;
break;
case 4:
std::cout << a << std::endl;
i += 2;
break;
case 5:
i = a != 0 ? b : i + 3;
break;
case 6:
i = a == 0 ? b : i + 3;
break;
case 7:
opcode[c] = a < b ? 1 : 0;
i += 4;
break;
case 8:
opcode[c] = a == b ? 1 : 0;
i += 4;
break;
case 9:
relative_base += a;
i += 2;
break;
default:
break;
}
}
}
int main() {
std::vector<long long int> vec;
vec.reserve(1000000);
long long int n, input;
char c;
std::cout << "Input ID: ";
std::cin >> input;
while (std::cin >> n) {
vec.push_back(n);
std::cin >> c;
}
parse(input, vec);
}