Guest User

AOC 2025/11

a guest
Dec 10th, 2025
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. namespace AdventOfCode.Y2025.Day11;
  2.  
  3. [ProblemName("Reactor")]
  4. class Solution : Solver
  5. {
  6.  
  7. public long PartOne(string input)
  8. {
  9. return CountPaths(Parse(input), "you", "out", [], true, true);
  10. }
  11.  
  12. public long? PartTwo(string input)
  13. {
  14. return CountPaths(Parse(input), "svr", "out", []);
  15. }
  16.  
  17. static Dictionary<string, List<string>> Parse(string input)
  18. {
  19. Dictionary<string, List<string>> graph = [];
  20.  
  21. var lines = input.Split('\n');
  22.  
  23. foreach (var line in lines)
  24. {
  25. var parts = line.Split(' ');
  26. graph[parts[0].Split(':')[0]] = parts[1..parts.Length].ToList();
  27. foreach (var p in parts[1..parts.Length])
  28. {
  29. if (!graph.ContainsKey(p))
  30. {
  31. graph[p] = [];
  32. }
  33. }
  34. }
  35. return graph;
  36. }
  37.  
  38.  
  39. static Dictionary<(string, string, HashSet<string>, bool, bool), long> memo = [];
  40. private static long CountPaths(Dictionary<string, List<string>> graph, string current, string target, HashSet<string> visited, bool dac = false, bool fft = false)
  41. {
  42. if (memo.TryGetValue((current, target, visited, dac, fft), out long res))
  43. {
  44. return res;
  45. }
  46.  
  47. if (current == "dac")
  48. dac = true;
  49. if (current == "fft")
  50. fft = true;
  51.  
  52. if (string.Equals(current, target, StringComparison.Ordinal))
  53. {
  54. if (dac && fft)
  55. {
  56. return 1;
  57. }
  58. return 0;
  59. }
  60.  
  61. if (visited.Contains(current))
  62. return 0;
  63.  
  64. visited.Add(current);
  65.  
  66. long total = 0;
  67. if (graph.TryGetValue(current, out var neighbors))
  68. {
  69. foreach (var n in neighbors)
  70. {
  71. total += CountPaths(graph, n, target, visited, dac, fft);
  72. }
  73. }
  74.  
  75. visited.Remove(current);
  76. memo[(current, target, visited, dac, fft)] = total;
  77. return total;
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment