-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday16.cpp
More file actions
304 lines (263 loc) · 8.13 KB
/
day16.cpp
File metadata and controls
304 lines (263 loc) · 8.13 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "day16.h"
#include "helpers.h"
#include <array>
#include <cassert>
#include <fstream>
#include <list>
#include <iostream>
#include <ranges>
#include <queue>
#include <string>
#include <vector>
namespace day16
{
long long solvePart1(std::ifstream& file);
long long solvePart2(std::ifstream& file);
void run_day(bool example)
{
std::cout << "Running day 16 " << (example ? "(example)" : "") << '\n';
const std::string fileName{ example ? "inputs/day16_example.txt" : "inputs/day16_real.txt" };
std::ifstream file{ fileName };
std::cout << "Part 1 answer: " << solvePart1(file) << '\n';
file.close();
file.open(fileName);
std::cout << "Part 2 answer: " << solvePart2(file) << '\n';
}
enum class Direction
{
north,
east,
south,
west
};
struct Beam
{
size_t x;
size_t y;
Direction dir;
void moveStep()
{
switch (dir) {
case Direction::north:
y--;
break;
case Direction::east:
x++;
break;
case Direction::south:
y++;
break;
case Direction::west:
x--;
break;
}
}
void updateDirWithCorner(const char corner)
{
if (corner == '\\')
{
switch (dir) {
case Direction::north:
dir = Direction::west;
break;
case Direction::east:
dir = Direction::south;
break;
case Direction::south:
dir = Direction::east;
break;
case Direction::west:
dir = Direction::north;
break;
}
}
else
{
switch (dir) {
case Direction::north:
dir = Direction::east;
break;
case Direction::east:
dir = Direction::north;
break;
case Direction::south:
dir = Direction::west;
break;
case Direction::west:
dir = Direction::south;
break;
}
}
}
};
struct Field
{
std::vector<std::string> field{};
// Interesting note: for some reasone writing to this triple nested array
// is much faster than updating a vector<vector<bool>> (used to have seperate energized variable).
std::vector<std::vector<std::array<bool, 4>>> directionPassed;
size_t width;
size_t height;
explicit Field(const std::vector<std::string>& f)
{
field = f;
height = f.size();
width = f[0].size();
resetField();
}
void fireBeam(size_t enterX, size_t enterY, Direction enterDir)
{
// Offset by one (reversed) step, since loops moves before marking
switch (enterDir) {
case Direction::north:
enterY++;
break;
case Direction::east:
enterX--;
break;
case Direction::south:
enterY--;
break;
case Direction::west:
enterX++;
break;
}
std::queue<Beam> beamQueue{};
beamQueue.push(Beam{ enterX, enterY, enterDir });
while (!beamQueue.empty())
{
Beam& b = beamQueue.front();
while(true)
{
b.moveStep();
// Use overflow to also monitor for going off low end
if (b.x >= width || b.y >= height)
{
break;
}
// loop detection
if (directionPassed[b.y][b.x][static_cast<unsigned int>(b.dir)])
{
break;
}
directionPassed[b.y][b.x][static_cast<unsigned int>(b.dir)] = true;
const char c{ field[b.y][b.x] };
if (c == '.')
{
continue;
}
if (c == '|')
{
if (b.dir == Direction::north || b.dir == Direction::south)
{
continue;
}
// Replace b with beams going up & down:
b.dir = Direction::north;
beamQueue.push(Beam{ b.x, b.y, Direction::south });
continue;
}
if (c == '-')
{
if (b.dir == Direction::west || b.dir == Direction::east)
{
continue;
}
// Replace b with beams going up & down:
b.dir = Direction::west;
beamQueue.push(Beam{ b.x, b.y, Direction::east });
continue;
}
b.updateDirWithCorner(c);
}
beamQueue.pop();
}
}
void resetField()
{
directionPassed = std::vector(height, std::vector(width, std::array<bool, 4>{}));
}
void printEnergized() const
{
std::cout << '\n';
for (const auto& row : directionPassed)
{
for (const auto b : row)
{
if (std::ranges::any_of(b, [](bool b){return b;}))
{
std::cout << '#';
}
else
{
std::cout << '.';
}
}
std::cout << '\n';
}
}
[[nodiscard]] long long score() const
{
long long sum{};
for (const auto& row : directionPassed)
{
for (const auto b : row)
{
for (size_t i = 0; i < 4; i++)
{
if (b[i])
{
sum++;
break;
}
}
}
}
return sum;
}
};
Field parseInput(std::ifstream& file)
{
std::vector<std::string> field{};
while(!file.eof())
{
std::string line;
std::getline(file, line);
field.push_back(line);
}
return Field( field );
}
long long solvePart1(std::ifstream& file)
{
auto field{ parseInput(file) };
field.fireBeam(0, 0, Direction::east);
// field.printEnergized();
return field.score();
}
long long solvePart2(std::ifstream& file)
{
auto field{ parseInput(file) };
long long maxScore{};
// Loop over left & right side:
for (size_t y = 0; y < field.height; y++)
{
field.resetField();
field.fireBeam(0, y, Direction::east);
maxScore = std::max(maxScore, field.score());
field.resetField();
field.fireBeam(field.width - 1, y, Direction::west);
maxScore = std::max(maxScore, field.score());
}
// Loop over top & bottom side:
for (size_t x = 0; x < field.width ; x++)
{
field.resetField();
field.fireBeam(x, 0, Direction::south);
maxScore = std::max(maxScore, field.score());
field.resetField();
field.fireBeam(x, field.height - 1, Direction::north);
maxScore = std::max(maxScore, field.score());
}
return maxScore;
}
}