Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace AdventOfCode.Y2025.Day11;
- [ProblemName("Reactor")]
- class Solution : Solver
- {
- public long PartOne(string input)
- {
- return CountPaths(Parse(input), "you", "out", [], true, true);
- }
- public long? PartTwo(string input)
- {
- return CountPaths(Parse(input), "svr", "out", []);
- }
- static Dictionary<string, List<string>> Parse(string input)
- {
- Dictionary<string, List<string>> graph = [];
- var lines = input.Split('\n');
- foreach (var line in lines)
- {
- var parts = line.Split(' ');
- graph[parts[0].Split(':')[0]] = parts[1..parts.Length].ToList();
- foreach (var p in parts[1..parts.Length])
- {
- if (!graph.ContainsKey(p))
- {
- graph[p] = [];
- }
- }
- }
- return graph;
- }
- static Dictionary<(string, string, HashSet<string>, bool, bool), long> memo = [];
- private static long CountPaths(Dictionary<string, List<string>> graph, string current, string target, HashSet<string> visited, bool dac = false, bool fft = false)
- {
- if (memo.TryGetValue((current, target, visited, dac, fft), out long res))
- {
- return res;
- }
- if (current == "dac")
- dac = true;
- if (current == "fft")
- fft = true;
- if (string.Equals(current, target, StringComparison.Ordinal))
- {
- if (dac && fft)
- {
- return 1;
- }
- return 0;
- }
- if (visited.Contains(current))
- return 0;
- visited.Add(current);
- long total = 0;
- if (graph.TryGetValue(current, out var neighbors))
- {
- foreach (var n in neighbors)
- {
- total += CountPaths(graph, n, target, visited, dac, fft);
- }
- }
- visited.Remove(current);
- memo[(current, target, visited, dac, fft)] = total;
- return total;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment