-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.cpp
205 lines (175 loc) · 4.17 KB
/
day17.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
/*
* day17.cpp
*
* Created on: Dec 17, 2019
* Author: sanja
*/
#include <iostream>
#include <iterator>
#include <fstream>
#include <vector>
#include <unordered_map>
#include <string>
const int size_of_m = 60;
char matrix[size_of_m][size_of_m];
int row, column;
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:
if(a == 10) {
column = 0;
row++;
} else {
matrix[row][column] = (char) a;
++column;
}
std::cout << (char)a;
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;
}
}
}
long long int parse_2(std::vector<long long int> input, std::vector<long long int> opcode) {
long long int i = 0, relative_base = 0, input_index = 0;
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[input_index++];
else opcode[opcode[i + 1]] = input[input_index++];
i += 2;
break;
case 4:
if(a > 127) return a;
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 collect_Dust(std::vector<long long int> opcode)
{
std::string main_routine = "A,A,C,B,C,B,C,B,A,B\n";
std::string a = "R,6,L,8,R,8\n";
std::string b = "L,8,R,6,L,10,L,10\n";
std::string c = "R,4,R,6,R,6,R,4,R,4\n";
std::string video = "n\n";
std::string all = main_routine + a + b + c + video;
std::vector<long long int> input_data;
input_data.reserve(all.size());
for (char c : all)
{
input_data.push_back(static_cast<long long int>(c));
}
return parse_2(input_data, opcode);
}
bool isIntersection(int i, int j)
{
return matrix[i][j+1] == '#' && matrix[i][j-1] == '#' && matrix[i+1][j] == '#' && matrix[i-1][j] == '#';
}
int find_Intersections()
{
int sum = 0;
for(int i = 0; i < size_of_m; i++)
{
for(int j = 0; j < size_of_m; j++)
{
if(matrix[i][j] == '#' && isIntersection(i, j))
sum += i*j;
}
}
return sum;
}
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);
parse(0, vec);
std::cout << find_Intersections() << std::endl;
std::cout << collect_Dust(vec) << std::endl;
}