aboutsummaryrefslogtreecommitdiff
path: root/2025/src/day11.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2025/src/day11.cpp')
-rw-r--r--2025/src/day11.cpp29
1 files changed, 27 insertions, 2 deletions
diff --git a/2025/src/day11.cpp b/2025/src/day11.cpp
index 47440f9..1bf8d44 100644
--- a/2025/src/day11.cpp
+++ b/2025/src/day11.cpp
@@ -64,17 +64,41 @@ part1(const Graph& graph)
64 } 64 }
65 65
66 long count = 0; 66 long count = 0;
67
68 for ( const auto& node: graph.at(src) ) { 67 for ( const auto& node: graph.at(src) ) {
69 count += dfs(node); 68 count += dfs(node);
70 } 69 }
71
72 return cache[src] = count; 70 return cache[src] = count;
73 }; 71 };
74 72
75 cout << "Part 1: " << dfs("you") << '\n'; 73 cout << "Part 1: " << dfs("you") << '\n';
76} 74}
77 75
76void
77part2(const Graph& graph)
78{
79 map<tuple<string, bool, bool>, long> cache;
80
81 function<long(const string&, bool, bool)> dfs = [&](const string& src, bool dac_visited, bool fft_visited) {
82 if ( src == "out" ) {
83 return (dac_visited && fft_visited) ? 1L : 0L;
84 }
85
86 const auto args = make_tuple(src, dac_visited, fft_visited);
87
88 if ( cache.contains(args) ) {
89 return cache.at(args);
90 }
91
92 long count = 0;
93 for ( const auto& node: graph.at(src) ) {
94 count += dfs(node, dac_visited || node == "dac", fft_visited || node == "fft");
95 }
96 return cache[args] = count;
97 };
98
99 cout << "Part 2: " << dfs("svr", false, false) << '\n';
100}
101
78} // namespace 102} // namespace
79 103
80int 104int
@@ -82,4 +106,5 @@ main()
82{ 106{
83 auto graph = read_file("data/day11.txt"); 107 auto graph = read_file("data/day11.txt");
84 part1(graph); 108 part1(graph);
109 part2(graph);
85} 110}