From aa823e8292ac80127c903aff8c783cb5d2f875db Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 15 Dec 2024 12:13:52 +0100 Subject: aoc 2024, day 15, part 1, speedup --- 2024/src/day15.cpp | 28 ++++++++++++---------------- 1 file 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 @@ #include #include #include +#include #include #include #include @@ -50,6 +51,7 @@ read_file(string_view filename) return { room, boxes, movements, start }; } +/* void print(const room_type& room, const boxes_type& boxes, const pos_type& pos) { @@ -72,6 +74,7 @@ print(const room_type& room, const boxes_type& boxes, const pos_type& pos) } cout << endl; } +*/ optional can_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) get<0>(start) += dx; get<1>(start) += dy; - boxes_type new_boxes; - for ( const auto& box: boxes ) { - if ( bitw->contains(box) ) { - const auto [x, y] = box; - new_boxes.emplace(x + dx, y + dy); - bitw->erase(box); - } - else { - new_boxes.insert(box); - } + for ( const auto& box: bitw.value() ) { + boxes.erase(box); + } + + for ( auto [x, y]: bitw.value() ) { + boxes.emplace(x + dx, y + dy); } - boxes = new_boxes; } } - size_t sum = 0; - for (const auto& [x, y]: boxes) { - sum += x + 100 * y; - } - cout << sum << endl; + cout << accumulate(boxes.begin(), boxes.end(), 0UL, [](auto init, const auto& box) { + return init + get<0>(box) + 100 * get<1>(box); + }) << endl; } int -- cgit v1.3