-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13.cpp
189 lines (164 loc) · 3.7 KB
/
day13.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
/*
* day13.cpp
*
* Created on: Dec 13, 2019
* Author: sanja
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map>
#include <iterator>
const int END_X = -1;
const int END_Y = 0;
const int PADDLE = 3;
const int BALL = 4;
long long int parse(long long int input, std::vector<long long int> opcode)
{
long long int i = 0, relative_base = 0;
int index = 1, blocks = 0;;
std::vector<std::vector<int>> objects;
std::vector<int> current;
current.resize(3);
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:
current[index%3] = a;
if(index%3 == 0)
{
objects.push_back(current);
if(a == 2) ++blocks;
}
++index;
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;
}
}
return blocks;
}
long long int parse_2(long long int input, std::vector<long long int> opcode)
{
long long int i = 0, relative_base = 0;
int index = 1, points = 0, paddle, ball, x, y;
opcode[0] = 2;
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:
if(index % 3 == 1)
{
x = a;
}
else if(index % 3 == 2)
{
y = a;
}
else
{
paddle = a == PADDLE ? x : paddle;
ball = a == BALL ? x : ball;
points = (x == END_X && y == END_Y) ? a : points;
}
input = (paddle < ball) ? 1 : (paddle > ball) ? -1 : 0;
++index;
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;
}
}
return points;
}
int main() {
const std::string input_file{ "input.txt" };
std::ifstream ifs(input_file);
std::vector<long long> vec{ std::istream_iterator<long long>(ifs), std::istream_iterator<long long>() };
vec.resize(100000);
std::cout << parse(0, vec) << std::endl;
std::cout << parse_2(0, vec) << std::endl;
}