diff options
Diffstat (limited to '2023/src')
| -rw-r--r-- | 2023/src/day21.cpp | 33 |
1 files changed, 8 insertions, 25 deletions
diff --git a/2023/src/day21.cpp b/2023/src/day21.cpp index c633155..21a45f7 100644 --- a/2023/src/day21.cpp +++ b/2023/src/day21.cpp | |||
| @@ -1,7 +1,5 @@ | |||
| 1 | #include <cstddef> | ||
| 2 | #include <fstream> | 1 | #include <fstream> |
| 3 | #include <iostream> | 2 | #include <iostream> |
| 4 | #include <queue> | ||
| 5 | #include <set> | 3 | #include <set> |
| 6 | #include <string> | 4 | #include <string> |
| 7 | #include <vector> | 5 | #include <vector> |
| @@ -56,35 +54,20 @@ find_neighbours(position pos, const vector<string>& lines) | |||
| 56 | } | 54 | } |
| 57 | 55 | ||
| 58 | size_t | 56 | size_t |
| 59 | count(vector<string> lines, position start, size_t rounds) | 57 | count(const vector<string>& lines, position start, size_t rounds) |
| 60 | { | 58 | { |
| 61 | queue<position> positions; | 59 | set<position> positions; |
| 62 | positions.emplace(start); | 60 | positions.emplace(start); |
| 63 | 61 | ||
| 64 | size_t sum = 0; | ||
| 65 | for ( size_t round = 0; round != rounds; ++round ) { | 62 | for ( size_t round = 0; round != rounds; ++round ) { |
| 66 | set<position> next_positions; | 63 | set<position> new_positions; |
| 67 | 64 | for ( const auto& position: positions ) { | |
| 68 | sum = 0; | 65 | const auto neighbours = find_neighbours(position, lines); |
| 69 | while ( !positions.empty() ) { | 66 | new_positions.insert(neighbours.begin(), neighbours.end()); |
| 70 | const auto [curr_row, curr_col] = positions.front(); | ||
| 71 | lines[curr_row][curr_col] = '.'; | ||
| 72 | |||
| 73 | const auto neighbours = find_neighbours(positions.front(), lines); | ||
| 74 | positions.pop(); | ||
| 75 | |||
| 76 | for ( const auto& [row, col]: neighbours ) { | ||
| 77 | lines[row][col] = 'O'; | ||
| 78 | next_positions.emplace(row, col); | ||
| 79 | ++sum; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | for ( const auto& position: next_positions ) { | ||
| 84 | positions.emplace(position); | ||
| 85 | } | 67 | } |
| 68 | positions = std::move(new_positions); | ||
| 86 | } | 69 | } |
| 87 | return sum; | 70 | return positions.size(); |
| 88 | } | 71 | } |
| 89 | 72 | ||
| 90 | void | 73 | void |
