aboutsummaryrefslogtreecommitdiff
path: root/2024
diff options
context:
space:
mode:
Diffstat (limited to '2024')
-rw-r--r--2024/src/day15.cpp28
1 files changed, 12 insertions, 16 deletions
diff --git a/2024/src/day15.cpp b/2024/src/day15.cpp
index e11661e..5c2449b 100644
--- a/2024/src/day15.cpp
+++ b/2024/src/day15.cpp
@@ -1,6 +1,7 @@
1#include <fstream> 1#include <fstream>
2#include <iostream> 2#include <iostream>
3#include <map> 3#include <map>
4#include <numeric>
4#include <optional> 5#include <optional>
5#include <set> 6#include <set>
6#include <string> 7#include <string>
@@ -50,6 +51,7 @@ read_file(string_view filename)
50 return { room, boxes, movements, start }; 51 return { room, boxes, movements, start };
51} 52}
52 53
54/*
53void 55void
54print(const room_type& room, const boxes_type& boxes, const pos_type& pos) 56print(const room_type& room, const boxes_type& boxes, const pos_type& pos)
55{ 57{
@@ -72,6 +74,7 @@ print(const room_type& room, const boxes_type& boxes, const pos_type& pos)
72 } 74 }
73 cout << endl; 75 cout << endl;
74} 76}
77*/
75 78
76optional<boxes_type> 79optional<boxes_type>
77can_move(const room_type& room, const boxes_type& boxes, pos_type start, pos_type direction) // NOLINT 80can_move(const room_type& room, const boxes_type& boxes, pos_type start, pos_type direction) // NOLINT
@@ -114,26 +117,19 @@ part1(const puzzle_type& puzzle)
114 get<0>(start) += dx; 117 get<0>(start) += dx;
115 get<1>(start) += dy; 118 get<1>(start) += dy;
116 119
117 boxes_type new_boxes; 120 for ( const auto& box: bitw.value() ) {
118 for ( const auto& box: boxes ) { 121 boxes.erase(box);
119 if ( bitw->contains(box) ) { 122 }
120 const auto [x, y] = box; 123
121 new_boxes.emplace(x + dx, y + dy); 124 for ( auto [x, y]: bitw.value() ) {
122 bitw->erase(box); 125 boxes.emplace(x + dx, y + dy);
123 }
124 else {
125 new_boxes.insert(box);
126 }
127 } 126 }
128 boxes = new_boxes;
129 } 127 }
130 } 128 }
131 129
132 size_t sum = 0; 130 cout << accumulate(boxes.begin(), boxes.end(), 0UL, [](auto init, const auto& box) {
133 for (const auto& [x, y]: boxes) { 131 return init + get<0>(box) + 100 * get<1>(box);
134 sum += x + 100 * y; 132 }) << endl;
135 }
136 cout << sum << endl;
137} 133}
138 134
139int 135int