diff options
Diffstat (limited to '2025')
| -rw-r--r-- | 2025/src/day04.cpp | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/2025/src/day04.cpp b/2025/src/day04.cpp index 295ed75..ef3b7f5 100644 --- a/2025/src/day04.cpp +++ b/2025/src/day04.cpp | |||
| @@ -1,6 +1,9 @@ | |||
| 1 | #include <array> | ||
| 1 | #include <filesystem> | 2 | #include <filesystem> |
| 2 | #include <fstream> | 3 | #include <fstream> |
| 3 | #include <iostream> | 4 | #include <iostream> |
| 5 | #include <map> | ||
| 6 | #include <queue> | ||
| 4 | #include <set> | 7 | #include <set> |
| 5 | #include <string> | 8 | #include <string> |
| 6 | #include <tuple> | 9 | #include <tuple> |
| @@ -30,7 +33,7 @@ read_file(const filesystem::path& filename) | |||
| 30 | return grid; | 33 | return grid; |
| 31 | } | 34 | } |
| 32 | 35 | ||
| 33 | set<Pos> | 36 | array<Pos, 8> |
| 34 | get_neighbours(const Pos& pos) | 37 | get_neighbours(const Pos& pos) |
| 35 | { | 38 | { |
| 36 | auto [col, row] = pos; | 39 | auto [col, row] = pos; |
| @@ -47,40 +50,35 @@ get_neighbours(const Pos& pos) | |||
| 47 | }; | 50 | }; |
| 48 | } | 51 | } |
| 49 | 52 | ||
| 53 | size_t | ||
| 54 | get_nneighbours(const set<Pos>& grid, const Pos& pos) | ||
| 55 | { | ||
| 56 | return (size_t) ranges::count_if(get_neighbours(pos), [&](const auto& neighbour) { return grid.contains(neighbour); }); | ||
| 57 | } | ||
| 58 | |||
| 50 | bool | 59 | bool |
| 51 | is_accessable(const set<Pos>& grid, const Pos& pos) | 60 | is_accessable(const set<Pos>& grid, const Pos& pos) |
| 52 | { | 61 | { |
| 53 | long nneighbours = 0; | 62 | return get_nneighbours(grid, pos) < 4; |
| 54 | for ( const auto neighbour: get_neighbours(pos) ) { | ||
| 55 | if ( grid.contains(neighbour) ) { | ||
| 56 | ++nneighbours; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | return nneighbours < 4; | ||
| 60 | } | 63 | } |
| 61 | 64 | ||
| 62 | void | 65 | void |
| 63 | part1(const set<Pos>& grid) | 66 | part1(const set<Pos>& grid) |
| 64 | { | 67 | { |
| 65 | long count = 0; | 68 | auto count = ranges::count_if(grid, [&](const auto& pos) { return is_accessable(grid, pos); }); |
| 66 | for ( const auto pos: grid ) { | ||
| 67 | if ( is_accessable(grid, pos) ) { | ||
| 68 | ++count; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | cout << "Part 1: " << count << '\n'; | 69 | cout << "Part 1: " << count << '\n'; |
| 72 | } | 70 | } |
| 73 | 71 | ||
| 74 | void | 72 | void |
| 75 | part2(set<Pos> grid) | 73 | part2(set<Pos> grid) |
| 76 | { | 74 | { |
| 77 | auto old_size = grid.size(); | 75 | const auto old_size = grid.size(); |
| 78 | while ( true ) { | 76 | while ( true ) { |
| 79 | set<Pos> remove; | 77 | vector<Pos> remove; |
| 80 | 78 | ||
| 81 | for ( const auto& pos: grid ) { | 79 | for ( const auto& pos: grid ) { |
| 82 | if ( is_accessable(grid, pos) ) { | 80 | if ( is_accessable(grid, pos) ) { |
| 83 | remove.insert(pos); | 81 | remove.emplace_back(pos); |
| 84 | } | 82 | } |
| 85 | } | 83 | } |
| 86 | 84 | ||
| @@ -92,6 +90,7 @@ part2(set<Pos> grid) | |||
| 92 | grid.erase(pos); | 90 | grid.erase(pos); |
| 93 | } | 91 | } |
| 94 | } | 92 | } |
| 93 | |||
| 95 | cout << "Part 2: " << old_size - grid.size() << '\n'; | 94 | cout << "Part 2: " << old_size - grid.size() << '\n'; |
| 96 | } | 95 | } |
| 97 | 96 | ||
