aboutsummaryrefslogtreecommitdiff
path: root/2025/src/day07.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2025/src/day07.cpp')
-rw-r--r--2025/src/day07.cpp34
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
53void
54part2(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
53int 86int
@@ -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}