diff options
Diffstat (limited to '2025/src/day04.cpp')
| -rw-r--r-- | 2025/src/day04.cpp | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/2025/src/day04.cpp b/2025/src/day04.cpp new file mode 100644 index 0000000..295ed75 --- /dev/null +++ b/2025/src/day04.cpp | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | #include <filesystem> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <set> | ||
| 5 | #include <string> | ||
| 6 | #include <tuple> | ||
| 7 | |||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | namespace { | ||
| 11 | |||
| 12 | using Pos = tuple<size_t, size_t>; | ||
| 13 | |||
| 14 | set<Pos> | ||
| 15 | read_file(const filesystem::path& filename) | ||
| 16 | { | ||
| 17 | ifstream file{ filename }; | ||
| 18 | set<Pos> grid; | ||
| 19 | |||
| 20 | size_t row = 0; | ||
| 21 | for ( string line; getline(file, line); ) { | ||
| 22 | for ( size_t col = 0; col != line.length(); ++col ) { | ||
| 23 | if ( line[col] == '@' ) { | ||
| 24 | grid.emplace(col, row); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | ++row; | ||
| 28 | } | ||
| 29 | |||
| 30 | return grid; | ||
| 31 | } | ||
| 32 | |||
| 33 | set<Pos> | ||
| 34 | get_neighbours(const Pos& pos) | ||
| 35 | { | ||
| 36 | auto [col, row] = pos; | ||
| 37 | |||
| 38 | return { | ||
| 39 | Pos{ col - 1, row }, | ||
| 40 | Pos{ col - 1, row - 1 }, | ||
| 41 | Pos{ col, row - 1 }, | ||
| 42 | Pos{ col + 1, row - 1 }, | ||
| 43 | Pos{ col + 1, row }, | ||
| 44 | Pos{ col + 1, row + 1 }, | ||
| 45 | Pos{ col, row + 1 }, | ||
| 46 | Pos{ col - 1, row + 1 }, | ||
| 47 | }; | ||
| 48 | } | ||
| 49 | |||
| 50 | bool | ||
| 51 | is_accessable(const set<Pos>& grid, const Pos& pos) | ||
| 52 | { | ||
| 53 | long nneighbours = 0; | ||
| 54 | for ( const auto neighbour: get_neighbours(pos) ) { | ||
| 55 | if ( grid.contains(neighbour) ) { | ||
| 56 | ++nneighbours; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | return nneighbours < 4; | ||
| 60 | } | ||
| 61 | |||
| 62 | void | ||
| 63 | part1(const set<Pos>& grid) | ||
| 64 | { | ||
| 65 | long count = 0; | ||
| 66 | for ( const auto pos: grid ) { | ||
| 67 | if ( is_accessable(grid, pos) ) { | ||
| 68 | ++count; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | cout << "Part 1: " << count << '\n'; | ||
| 72 | } | ||
| 73 | |||
| 74 | void | ||
| 75 | part2(set<Pos> grid) | ||
| 76 | { | ||
| 77 | auto old_size = grid.size(); | ||
| 78 | while ( true ) { | ||
| 79 | set<Pos> remove; | ||
| 80 | |||
| 81 | for ( const auto& pos: grid ) { | ||
| 82 | if ( is_accessable(grid, pos) ) { | ||
| 83 | remove.insert(pos); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | if ( remove.empty() ) { | ||
| 88 | break; | ||
| 89 | } | ||
| 90 | |||
| 91 | for ( const auto& pos: remove ) { | ||
| 92 | grid.erase(pos); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | cout << "Part 2: " << old_size - grid.size() << '\n'; | ||
| 96 | } | ||
| 97 | |||
| 98 | } // namespace | ||
| 99 | |||
| 100 | int | ||
| 101 | main() | ||
| 102 | { | ||
| 103 | auto grid = read_file("data/day04.txt"); | ||
| 104 | part1(grid); | ||
| 105 | part2(grid); | ||
| 106 | } | ||
