aboutsummaryrefslogtreecommitdiff
path: root/2024/src/day15.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-12-15 22:32:42 +0100
committerThomas Schmucker <ts@its1.de>2024-12-15 22:32:42 +0100
commit593e2531315e66a3add1644fe838286fbea478b8 (patch)
tree4a008838b59333255b72bd0e41bf3c11330ea539 /2024/src/day15.cpp
parente733679389d4facd44055dc7844a0441c5bee02e (diff)
downloadadvent-of-code-593e2531315e66a3add1644fe838286fbea478b8.tar.gz
advent-of-code-593e2531315e66a3add1644fe838286fbea478b8.tar.bz2
advent-of-code-593e2531315e66a3add1644fe838286fbea478b8.zip
cleanup
Diffstat (limited to '2024/src/day15.cpp')
-rw-r--r--2024/src/day15.cpp72
1 files changed, 28 insertions, 44 deletions
diff --git a/2024/src/day15.cpp b/2024/src/day15.cpp
index 94b8a2b..5e29711 100644
--- a/2024/src/day15.cpp
+++ b/2024/src/day15.cpp
@@ -55,43 +55,26 @@ read_file(string_view filename)
55} 55}
56 56
57puzzle2_type 57puzzle2_type
58read_file2(string_view filename) 58convert(const puzzle_type& puzzle)
59{ 59{
60 fstream input{ filename }; 60 const auto& [room, boxes, movements, start] = puzzle;
61 room_type room;
62 boxes2_type boxes;
63 string movements;
64 pos_type start;
65 61
66 size_t yPos = 0; 62 room_type room2;
67 for ( string line; getline(input, line); ) { 63 for ( const auto& [x, y]: room ) {
68 if ( line.empty() ) { 64 room2.emplace(x * 2, y);
69 continue; 65 room2.emplace(x * 2 + 1, y);
70 } 66 }
71 67
72 if ( line[0] == '#' ) { 68 boxes2_type boxes2;
73 for ( size_t xPos = 0; xPos != line.size(); ++xPos ) { 69 for ( const auto& [x, y]: boxes ) {
74 auto chr = line.at(xPos); 70 boxes2.insert({ pos_type{ x * 2, y }, true });
75 if ( chr == '#' ) { 71 boxes2.insert({ pos_type{ x * 2 + 1, y }, false });
76 room.emplace(xPos * 2, yPos);
77 room.emplace(xPos * 2 + 1, yPos);
78 }
79 else if ( chr == 'O' ) {
80 boxes.insert({ pos_type(xPos * 2, yPos), true });
81 boxes.insert({ pos_type(xPos * 2 + 1, yPos), false });
82 }
83 else if ( chr == '@' ) {
84 start = { xPos * 2, yPos };
85 }
86 }
87 ++yPos;
88 }
89 else {
90 movements += line;
91 }
92 } 72 }
93 73
94 return { room, boxes, movements, start }; 74 const auto& [x, y] = start;
75 pos_type start2{ x * 2, y };
76
77 return { room2, boxes2, movements, start2 };
95} 78}
96 79
97void 80void
@@ -181,7 +164,7 @@ can_move(const room_type& room, const boxes_type& boxes, pos_type start, pos_typ
181void 164void
182part1(const puzzle_type& puzzle) 165part1(const puzzle_type& puzzle)
183{ 166{
184 map<char, pos_type> deltas = { 167 const map<char, pos_type> deltas = {
185 { '<', { -1, 0 } }, // Left 168 { '<', { -1, 0 } }, // Left
186 { '^', { 0, -1 } }, // Up 169 { '^', { 0, -1 } }, // Up
187 { '>', { 1, 0 } }, // Right 170 { '>', { 1, 0 } }, // Right
@@ -191,7 +174,7 @@ part1(const puzzle_type& puzzle)
191 auto [room, boxes, movements, start] = puzzle; 174 auto [room, boxes, movements, start] = puzzle;
192 175
193 for ( auto chr: movements ) { 176 for ( auto chr: movements ) {
194 const auto delta = deltas[chr]; 177 const auto delta = deltas.at(chr);
195 if ( auto bitw = can_move(room, boxes, start, delta) ) { 178 if ( auto bitw = can_move(room, boxes, start, delta) ) {
196 const auto [dx, dy] = delta; 179 const auto [dx, dy] = delta;
197 180
@@ -228,12 +211,13 @@ get_both_parts(const boxes2_type& boxes, pos_type pos)
228optional<boxes2_type> 211optional<boxes2_type>
229can_move(const room_type& room, const boxes2_type& all_boxes, pos_type start, pos_type direction) // NOLINT 212can_move(const room_type& room, const boxes2_type& all_boxes, pos_type start, pos_type direction) // NOLINT
230{ 213{
231 function<optional<boxes2_type>(const boxes2_type&, size_t)> can_move_up_down = [&room, &all_boxes, &can_move_up_down](const boxes2_type& boxes, size_t dy) -> optional<boxes2_type> { 214 function<optional<boxes2_type>(const boxes2_type&, size_t)> can_move_up_down =
215 [&room, &all_boxes, &can_move_up_down](const boxes2_type& boxes, const size_t dy) -> optional<boxes2_type> {
232 boxes2_type candidates; 216 boxes2_type candidates;
233 217
234 for ( const auto& [pos, b]: boxes ) { 218 for ( const auto& [pos, b]: boxes ) {
235 const auto [x, y] = pos; 219 const auto [x, y] = pos;
236 pos_type try_pos(x, y + dy); 220 const pos_type try_pos(x, y + dy);
237 221
238 if ( room.contains(try_pos) ) { 222 if ( room.contains(try_pos) ) {
239 return nullopt; 223 return nullopt;
@@ -290,7 +274,7 @@ can_move(const room_type& room, const boxes2_type& all_boxes, pos_type start, po
290void 274void
291part2(const puzzle2_type& puzzle) 275part2(const puzzle2_type& puzzle)
292{ 276{
293 map<char, pos_type> deltas = { 277 const map<char, pos_type> deltas = {
294 { '<', { -1, 0 } }, // Left 278 { '<', { -1, 0 } }, // Left
295 { '^', { 0, -1 } }, // Up 279 { '^', { 0, -1 } }, // Up
296 { '>', { 1, 0 } }, // Right 280 { '>', { 1, 0 } }, // Right
@@ -300,7 +284,7 @@ part2(const puzzle2_type& puzzle)
300 auto [room, boxes, movements, start] = puzzle; 284 auto [room, boxes, movements, start] = puzzle;
301 285
302 for ( auto chr: movements ) { 286 for ( auto chr: movements ) {
303 const auto delta = deltas[chr]; 287 const auto delta = deltas.at(chr);
304 if ( auto bitw = can_move(room, boxes, start, delta) ) { 288 if ( auto bitw = can_move(room, boxes, start, delta) ) {
305 const auto [dx, dy] = delta; 289 const auto [dx, dy] = delta;
306 290
@@ -320,15 +304,15 @@ part2(const puzzle2_type& puzzle)
320 304
321 cout << accumulate(boxes.begin(), boxes.end(), 0UL, [](auto init, const auto& box) { 305 cout << accumulate(boxes.begin(), boxes.end(), 0UL, [](auto init, const auto& box) {
322 const auto& [pos, b] = box; 306 const auto& [pos, b] = box;
323 if ( !b ) { 307 const auto [x, y] = pos;
324 return init; 308 return init + (x + 100 * y) * b;
325 }
326 return init + get<0>(pos) + 100 * get<1>(pos);
327 }) << endl; 309 }) << endl;
328} 310}
311
329int 312int
330main() 313main()
331{ 314{
332 part1(read_file("data/day15.txt")); 315 const auto puzzle = read_file("data/day15.txt");
333 part2(read_file2("data/day15.txt")); 316 part1(puzzle);
317 part2(convert(puzzle));
334} 318}