diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-12-07 09:30:23 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-12-07 09:30:23 +0100 |
| commit | 662e2317c7ff77236601c32b3b204fe89ac5cc5d (patch) | |
| tree | ee8ada55a396d7094ddb7386a37360093b4dbf0d /2025 | |
| parent | e8f91f0bfb65fc6d2402e8f47270d2ae884ca724 (diff) | |
| download | advent-of-code-662e2317c7ff77236601c32b3b204fe89ac5cc5d.tar.gz advent-of-code-662e2317c7ff77236601c32b3b204fe89ac5cc5d.tar.bz2 advent-of-code-662e2317c7ff77236601c32b3b204fe89ac5cc5d.zip | |
aoc 2025, day 7, part 2
Diffstat (limited to '2025')
| -rw-r--r-- | 2025/src/day07.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/2025/src/day07.cpp b/2025/src/day07.cpp index ca73d87..e902a43 100644 --- a/2025/src/day07.cpp +++ b/2025/src/day07.cpp | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | #include <filesystem> | 1 | #include <filesystem> |
| 2 | #include <fstream> | 2 | #include <fstream> |
| 3 | #include <functional> | ||
| 3 | #include <iostream> | 4 | #include <iostream> |
| 5 | #include <map> | ||
| 4 | #include <set> | 6 | #include <set> |
| 5 | #include <string> | 7 | #include <string> |
| 6 | #include <vector> | 8 | #include <vector> |
| @@ -48,6 +50,37 @@ part1(const vector<string>& grid) | |||
| 48 | cout << "Part 1: " << count << '\n'; | 50 | cout << "Part 1: " << count << '\n'; |
| 49 | } | 51 | } |
| 50 | 52 | ||
| 53 | void | ||
| 54 | part2(const vector<string>& grid) | ||
| 55 | { | ||
| 56 | using Pos = tuple<size_t, size_t>; | ||
| 57 | |||
| 58 | map<Pos, long> cache; | ||
| 59 | function<long(const Pos&)> walk = [&](const Pos& pos) { | ||
| 60 | if ( cache.contains(pos) ) { | ||
| 61 | return cache.at(pos); | ||
| 62 | } | ||
| 63 | |||
| 64 | const auto [col, row] = pos; | ||
| 65 | |||
| 66 | if ( row == grid.size() ) { | ||
| 67 | return (cache[pos] = 1L); | ||
| 68 | } | ||
| 69 | |||
| 70 | if ( grid[row][col] == '^' ) { | ||
| 71 | const auto left = walk({ col - 1, row }); | ||
| 72 | const auto right = walk({ col + 1, row }); | ||
| 73 | |||
| 74 | return (cache[pos] = left + right); | ||
| 75 | } | ||
| 76 | |||
| 77 | return (cache[pos] = walk({ col, row + 1 })); | ||
| 78 | }; | ||
| 79 | |||
| 80 | const auto col = grid[0].find('S'); | ||
| 81 | cout << "Part 2: " << walk(Pos{ col, 0 }) << '\n'; | ||
| 82 | } | ||
| 83 | |||
| 51 | } // namespace | 84 | } // namespace |
| 52 | 85 | ||
| 53 | int | 86 | int |
| @@ -55,4 +88,5 @@ main() | |||
| 55 | { | 88 | { |
| 56 | auto grid = read_file("data/day07.txt"); | 89 | auto grid = read_file("data/day07.txt"); |
| 57 | part1(grid); | 90 | part1(grid); |
| 91 | part2(grid); | ||
| 58 | } | 92 | } |
