diff options
Diffstat (limited to '2025/src')
| -rw-r--r-- | 2025/src/day04.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/2025/src/day04.cpp b/2025/src/day04.cpp index ef3b7f5..75c86d9 100644 --- a/2025/src/day04.cpp +++ b/2025/src/day04.cpp | |||
| @@ -94,6 +94,46 @@ part2(set<Pos> grid) | |||
| 94 | cout << "Part 2: " << old_size - grid.size() << '\n'; | 94 | cout << "Part 2: " << old_size - grid.size() << '\n'; |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | void | ||
| 98 | part2_bfs(set<Pos> grid) | ||
| 99 | { | ||
| 100 | const auto old_size = grid.size(); | ||
| 101 | |||
| 102 | map<Pos, size_t> counts; | ||
| 103 | for ( const auto& pos: grid ) { | ||
| 104 | counts[pos] = get_nneighbours(grid, pos); | ||
| 105 | } | ||
| 106 | |||
| 107 | queue<Pos> queue; | ||
| 108 | for ( const auto& [pos, count]: counts ) { | ||
| 109 | if ( count < 4 ) { | ||
| 110 | queue.emplace(pos); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | while ( !queue.empty() ) { | ||
| 115 | const auto pos = queue.front(); | ||
| 116 | queue.pop(); | ||
| 117 | |||
| 118 | if ( !grid.contains(pos) ) { | ||
| 119 | continue; | ||
| 120 | } | ||
| 121 | |||
| 122 | grid.erase(pos); | ||
| 123 | |||
| 124 | for ( const auto& neighbour: get_neighbours(pos) ) { | ||
| 125 | if ( !grid.contains(neighbour) ) { | ||
| 126 | continue; | ||
| 127 | } | ||
| 128 | if ( --counts[neighbour] == 3 ) { | ||
| 129 | queue.emplace(neighbour); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | cout << "Part 2: " << old_size - grid.size() << '\n'; | ||
| 135 | } | ||
| 136 | |||
| 97 | } // namespace | 137 | } // namespace |
| 98 | 138 | ||
| 99 | int | 139 | int |
| @@ -102,4 +142,5 @@ main() | |||
| 102 | auto grid = read_file("data/day04.txt"); | 142 | auto grid = read_file("data/day04.txt"); |
| 103 | part1(grid); | 143 | part1(grid); |
| 104 | part2(grid); | 144 | part2(grid); |
| 145 | part2_bfs(grid); | ||
| 105 | } | 146 | } |
