diff options
Diffstat (limited to '2025')
| -rw-r--r-- | 2025/src/day11.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/2025/src/day11.cpp b/2025/src/day11.cpp index 1bf8d44..dc05ae3 100644 --- a/2025/src/day11.cpp +++ b/2025/src/day11.cpp | |||
| @@ -99,6 +99,39 @@ part2(const Graph& graph) | |||
| 99 | cout << "Part 2: " << dfs("svr", false, false) << '\n'; | 99 | cout << "Part 2: " << dfs("svr", false, false) << '\n'; |
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | void | ||
| 103 | part2_alt(const Graph& graph) | ||
| 104 | { | ||
| 105 | map<tuple<string, string>, long> cache; | ||
| 106 | |||
| 107 | function<long(const string&, const string&)> dfs = [&](const string& src, const string& dst) { | ||
| 108 | if ( src == dst ) { | ||
| 109 | return 1L; | ||
| 110 | } | ||
| 111 | |||
| 112 | const auto args = make_tuple(src, dst); | ||
| 113 | |||
| 114 | if ( cache.contains(args) ) { | ||
| 115 | return cache.at(args); | ||
| 116 | } | ||
| 117 | |||
| 118 | long count = 0; | ||
| 119 | if ( graph.contains(src) ) { | ||
| 120 | for ( const auto& node: graph.at(src) ) { | ||
| 121 | count += dfs(node, dst); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | return cache[args] = count; | ||
| 125 | }; | ||
| 126 | |||
| 127 | // clang-format off | ||
| 128 | const auto total = (dfs("svr", "dac") * dfs("dac", "fft") * dfs("fft", "out")) | ||
| 129 | + (dfs("svr", "fft") * dfs("fft", "dac") * dfs("dac", "out")); | ||
| 130 | // clang-format on | ||
| 131 | |||
| 132 | cout << "Part 2 (alternative): " << total << '\n'; | ||
| 133 | } | ||
| 134 | |||
| 102 | } // namespace | 135 | } // namespace |
| 103 | 136 | ||
| 104 | int | 137 | int |
| @@ -107,4 +140,5 @@ main() | |||
| 107 | auto graph = read_file("data/day11.txt"); | 140 | auto graph = read_file("data/day11.txt"); |
| 108 | part1(graph); | 141 | part1(graph); |
| 109 | part2(graph); | 142 | part2(graph); |
| 143 | part2_alt(graph); | ||
| 110 | } | 144 | } |
