diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-12-15 12:13:52 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-12-15 12:13:52 +0100 |
| commit | aa823e8292ac80127c903aff8c783cb5d2f875db (patch) | |
| tree | 8b4fc1a9517660eed95c203e80831cf6e65e252c /2024/src/day15.cpp | |
| parent | ba8010034e9873cce1a0e20397e69fab460c07ac (diff) | |
| download | advent-of-code-aa823e8292ac80127c903aff8c783cb5d2f875db.tar.gz advent-of-code-aa823e8292ac80127c903aff8c783cb5d2f875db.tar.bz2 advent-of-code-aa823e8292ac80127c903aff8c783cb5d2f875db.zip | |
aoc 2024, day 15, part 1, speedup
Diffstat (limited to '2024/src/day15.cpp')
| -rw-r--r-- | 2024/src/day15.cpp | 28 |
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 | /* | ||
| 53 | void | 55 | void |
| 54 | print(const room_type& room, const boxes_type& boxes, const pos_type& pos) | 56 | print(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 | ||
| 76 | optional<boxes_type> | 79 | optional<boxes_type> |
| 77 | can_move(const room_type& room, const boxes_type& boxes, pos_type start, pos_type direction) // NOLINT | 80 | 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) | |||
| 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 | ||
| 139 | int | 135 | int |
