-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq16.cpp
357 lines (311 loc) · 11.9 KB
/
q16.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <boost/algorithm/string/classification.hpp> // Include boost::for is_any_of
#include <boost/filesystem.hpp>
#include <fmt/format.h>
#include <fstream>
#include <gsl/gsl>
#include <iostream>
#include <numeric>
#include <set>
#include <vector>
// Custom classes -- TODO: move to seperate files
struct Location {
gsl::index row, col;
friend bool operator==(Location direction, Location other);
auto operator<=>(const Location& other) const {
if (row != other.row) {
return row <=> other.row;
}
else {
return col <=> other.col;
}
}
};
bool operator==(Location location, Location other) {
return location.row == other.row && location.col == other.col;
}
class Direction {
int row_delta, col_delta;
Direction(int row_delta, int col_delta) : row_delta{row_delta}, col_delta{col_delta} { }
public:
[[nodiscard]] static Direction up() { return {-1, 0}; } // -1 as, visually, row 0 is at the top
[[nodiscard]] static Direction down() { return {1, 0}; }
[[nodiscard]] static Direction left() { return {0, -1}; }
[[nodiscard]] static Direction right() { return {0, 1}; }
friend Location operator+(Location location, Direction direction);
friend bool operator==(Direction direction, Direction other);
auto operator<=>(const Direction& other) const { // all this for std::set later... worth it?
if (row_delta != other.row_delta) {
return row_delta <=> other.row_delta;
}
else {
return col_delta <=> other.col_delta;
}
}
};
bool operator==(Direction direction, Direction other) {
return direction.row_delta == other.row_delta && direction.col_delta == other.col_delta;
}
Location operator+(Location location, Direction direction) {
return {location.row + direction.row_delta, location.col + direction.col_delta};
}
struct Beam {
Location location = {0,0};
Direction direction = Direction::right();
auto operator<=>(const Beam& other) const {
if (location != other.location) {
return location <=> other.location;
} else {
return direction <=> other.direction;
}
}
};
class Square {
public:
enum class Type {EMPTY, SPLITTER_HORIZONTAL, SPLITTER_VERTICAL, MIRROR_FORWARDSLASH, MIRROR_BACKSLASH};
private:
Type to_type(char sqr) {
switch (sqr) {
case '.' : return Type::EMPTY;
case '-' : return Type::SPLITTER_HORIZONTAL;
case '|' : return Type::SPLITTER_VERTICAL;
case '/' : return Type::MIRROR_FORWARDSLASH;
case '\\': return Type::MIRROR_BACKSLASH;
default: throw std::invalid_argument("Invalid char");
}
}
Type m_type;
bool m_is_illuminated = false;
public:
explicit Square(char sym) : m_type{to_type(sym)} {}
[[nodiscard]] Type type() const { return m_type; }
void illuminate() { m_is_illuminated = true; }
void delluminate() { m_is_illuminated = false;}
bool is_illuminated() const {return m_is_illuminated; }
};
class Grid {
using Type = Square::Type;
std::vector<std::vector<Square>> grid;
std::vector<Beam> beams {Beam{}};
std::set<Beam> cache; // store prev beams to exit early if in loop
void handle_forward_mirror(Beam& beam) { // todo - feel there is a smarter way of expressing this
if (beam.direction == Direction::up()) {
beam.direction = Direction::right();
}
else if (beam.direction == Direction::right()) {
beam.direction = Direction::up();
}
else if (beam.direction == Direction::down()) {
beam.direction = Direction::left();
}
else {
beam.direction = Direction::down();
}
beam.location = beam.location + beam.direction;
}
void handle_backward_mirror(Beam& beam) {
if (beam.direction == Direction::up()) {
beam.direction = Direction::left();
}
else if (beam.direction == Direction::right()) {
beam.direction = Direction::down();
}
else if (beam.direction == Direction::down()) {
beam.direction = Direction::right();
}
else {
beam.direction = Direction::up();
}
beam.location = beam.location + beam.direction;
}
void move_beams_to_next_location() {
/// NB: happily moves to off-grid / invalid locations
std::vector<Beam> beams_from_split; // seperated to avoid changing beams.size() while iterating through
// helpers
auto handle_horizontal_splitter = [&](Beam& beam, gsl::index i) -> void {
const bool is_split = beam.direction == Direction::up() || beam.direction == Direction::down();
if (!is_split) {
beam.location = beam.location + beam.direction;
}
else {
Beam left_beam = {beam.location + Direction::left(), Direction::left()};
Beam right_beam = {beam.location + Direction::right(), Direction::right()};
beams.erase(begin(beams) + i); // remove original ...
beams_from_split.push_back(left_beam); // and create two beams
beams_from_split.push_back(right_beam);
}
};
auto handle_vertical_splitter = [&](Beam& beam, gsl::index i) -> void {
const bool is_split = beam.direction == Direction::left() || beam.direction == Direction::right();
if (!is_split) {
beam.location = beam.location + beam.direction;
}
else {
Beam up_beam = {beam.location + Direction::up(), Direction::up()};
Beam down_beam = {beam.location + Direction::down(), Direction::down()};
beams.erase(begin(beams) + i); // remove original ...
beams_from_split.push_back(up_beam); // and create two beams
beams_from_split.push_back(down_beam);
}
};
for (gsl::index i = 0; i < beams.size(); ++i) {
auto& beam = beams[i];
const Type beam_loc_type = grid[beam.location.row][beam.location.col].type();
switch (beam_loc_type) {
case Type::EMPTY : {
beam.location = beam.location + beam.direction;
break;
}
case Type::MIRROR_FORWARDSLASH : {
handle_forward_mirror(beam);
break;
}
case Type::MIRROR_BACKSLASH : {
handle_backward_mirror(beam);
break;
}
case Type::SPLITTER_HORIZONTAL : {
const auto& old_size = beams.size();
handle_horizontal_splitter(beam, i);
if (beams.size() != old_size) {
--i; // counteract deletion in handle_horizontal_splitter
}
break;
}
case Type::SPLITTER_VERTICAL : {
const auto& old_size = beams.size();
handle_vertical_splitter(beam, i);
if (beams.size() != old_size) {
--i; // counteract deletion in handle_vertical_splitter
}
break;
}
}
}
if (!beams_from_split.empty()){
beams.reserve(beams.size() + beams_from_split.size());
std::move(beams_from_split.begin(), beams_from_split.end(), std::back_inserter(beams));
}
}
void remove_unecessary_beams() {
const auto grid_rows = grid.size();
const auto grid_cols = grid[0].size();
std::erase_if(beams, [&](const Beam& b) {
return cache.contains(b) ||
!(0 <= b.location.row && b.location.row < grid_rows &&
0 <= b.location.col && b.location.col < grid_cols);
});
}
void illuminate_squares() {
/// Assumes valid grid locations
for (const Beam& beam : beams) {
grid[beam.location.row][beam.location.col].illuminate();
}
}
void delluminate_all() {
for (auto& row : grid) {
for (auto& col : row) {
col.delluminate();
}
}
}
public:
Grid(const std::vector<std::vector<Square>>& grid) : grid{grid} { }
void init_illumination_process() {
cache.clear();
delluminate_all();
remove_unecessary_beams();
for (const auto& b : beams) { // add starting beam
cache.insert(b);
}
illuminate_squares();
while (!beams.empty()) {
move_beams_to_next_location();
remove_unecessary_beams();
illuminate_squares();
for (const auto& b : beams) {
cache.insert(b);
}
}
}
unsigned no_of_illuminated_squares() const {
assert(cache.size() != 0 && "Forgot to init_illumination_process()");
return std::accumulate(cbegin(grid), cend(grid), size_t{0}, [](size_t acc, const std::vector<Square>& row) {
return acc + std::ranges::count_if(row, [](const Square& square){ return square.is_illuminated(); });
});
}
void set_starting_beam(const Beam& b){
beams.clear();
beams.push_back(b);
}
std::vector<Beam> get_starting_beams() const {
std::vector<Beam> beams;
for (gsl::index i = 0; i < grid.size(); ++i) {
const gsl::index max_row = gsl::narrow_cast<gsl::index>(grid.size()-1);
const gsl::index max_col = gsl::narrow_cast<gsl::index>(grid[0].size()-1);
beams.emplace_back(Location{i, 0}, Direction::right()); // left edge
beams.emplace_back(Location{i, max_col}, Direction::left()); // right edge
beams.emplace_back(Location{0, i}, Direction::down()); // top edge
beams.emplace_back(Location{max_row, i}, Direction::up()); // bottom edge
}
return beams;
}
};
// File Handling
bool is_valid_file(std::string_view file_path){
namespace fs = boost::filesystem;
if (!fs::exists(file_path) || !fs::is_regular_file(file_path)) {
fmt::print(stderr, "File does not exist or is not a regular file: {}\n", file_path);
return false;
}
return true;
}
auto tokenize(const std::vector<std::string> &lines_of_data) {
std::vector<std::vector<Square>> tokenized_lines_of_data;
for (const auto& line : lines_of_data) {
const std::vector<Square> tokens = [&](){
std::vector<Square> tokens;
for (char c : line) {
tokens.emplace_back(c);
}
return tokens;
}();
tokenized_lines_of_data.push_back(tokens);
}
return tokenized_lines_of_data;
}
template<typename T>
[[nodiscard]] std::vector<T> vectorize(std::ifstream& is) {
std::vector<T> vec;
T str;
while(std::getline(is, str)){
vec.push_back(str);
}
return vec;
}
Grid parse(std::string_view file_path) {
assert(is_valid_file(file_path));
std::ifstream data(file_path);
return Grid{tokenize(vectorize<std::string>(data))};
}
// solutions
unsigned part_1(Grid grid) {
grid.init_illumination_process();
return grid.no_of_illuminated_squares();
}
unsigned part_2(Grid grid) {
const std::vector<Beam> starting_beams = grid.get_starting_beams();
auto get_illuminated_squares = [&](const Beam& b) -> unsigned {
grid.set_starting_beam(b);
grid.init_illumination_process();
return grid.no_of_illuminated_squares();
};
std::vector<unsigned> scores(starting_beams.size());
std::ranges::transform(starting_beams, begin(scores), get_illuminated_squares);
return *std::ranges::max_element(scores);
}
int main() {
Grid grid = parse("../input.txt");
fmt::print("Part 1: {}\n", part_1(grid));
fmt::print("Part 2: {}\n", part_2(grid));
return 0;
}