diff options
Diffstat (limited to '2025/src')
| -rw-r--r-- | 2025/src/day07.cpp | 28 |
1 files changed, 10 insertions, 18 deletions
diff --git a/2025/src/day07.cpp b/2025/src/day07.cpp index e902a43..6592518 100644 --- a/2025/src/day07.cpp +++ b/2025/src/day07.cpp | |||
| @@ -14,21 +14,14 @@ namespace { | |||
| 14 | vector<string> | 14 | vector<string> |
| 15 | read_file(const filesystem::path& filename) | 15 | read_file(const filesystem::path& filename) |
| 16 | { | 16 | { |
| 17 | ifstream file{ filename }; | 17 | ifstream file{ filename }; |
| 18 | vector<string> grid; | 18 | return { istream_iterator<string>{ file }, {} }; |
| 19 | |||
| 20 | for ( string line; getline(file, line); ) { | ||
| 21 | grid.emplace_back(line); | ||
| 22 | } | ||
| 23 | |||
| 24 | return grid; | ||
| 25 | } | 19 | } |
| 26 | 20 | ||
| 27 | void | 21 | void |
| 28 | part1(const vector<string>& grid) | 22 | part1(const vector<string>& grid) |
| 29 | { | 23 | { |
| 30 | set<size_t> curr; | 24 | set<size_t> curr{ grid[0].find('S') }; |
| 31 | curr.insert(grid[0].find('S')); | ||
| 32 | 25 | ||
| 33 | int count = 0; | 26 | int count = 0; |
| 34 | for ( size_t i = 1; i < grid.size(); ++i ) { | 27 | for ( size_t i = 1; i < grid.size(); ++i ) { |
| @@ -36,11 +29,10 @@ part1(const vector<string>& grid) | |||
| 36 | 29 | ||
| 37 | auto next = curr; | 30 | auto next = curr; |
| 38 | 31 | ||
| 39 | for ( const auto pos: curr ) { | 32 | for ( const auto col: curr ) { |
| 40 | if ( row[pos] == '^' ) { | 33 | if ( row[col] == '^' ) { |
| 41 | next.erase(pos); | 34 | next.erase(col); |
| 42 | next.insert(pos - 1); | 35 | next.insert({ col - 1, col + 1 }); |
| 43 | next.insert(pos + 1); | ||
| 44 | ++count; | 36 | ++count; |
| 45 | } | 37 | } |
| 46 | } | 38 | } |
| @@ -55,7 +47,8 @@ part2(const vector<string>& grid) | |||
| 55 | { | 47 | { |
| 56 | using Pos = tuple<size_t, size_t>; | 48 | using Pos = tuple<size_t, size_t>; |
| 57 | 49 | ||
| 58 | map<Pos, long> cache; | 50 | map<Pos, long> cache; |
| 51 | |||
| 59 | function<long(const Pos&)> walk = [&](const Pos& pos) { | 52 | function<long(const Pos&)> walk = [&](const Pos& pos) { |
| 60 | if ( cache.contains(pos) ) { | 53 | if ( cache.contains(pos) ) { |
| 61 | return cache.at(pos); | 54 | return cache.at(pos); |
| @@ -77,8 +70,7 @@ part2(const vector<string>& grid) | |||
| 77 | return (cache[pos] = walk({ col, row + 1 })); | 70 | return (cache[pos] = walk({ col, row + 1 })); |
| 78 | }; | 71 | }; |
| 79 | 72 | ||
| 80 | const auto col = grid[0].find('S'); | 73 | cout << "Part 2: " << walk(Pos{ grid[0].find('S'), 0 }) << '\n'; |
| 81 | cout << "Part 2: " << walk(Pos{ col, 0 }) << '\n'; | ||
| 82 | } | 74 | } |
| 83 | 75 | ||
| 84 | } // namespace | 76 | } // namespace |
