2025 - Day 11 (Reactor)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3

#Advent of Code
#2025 Day 11, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input: list[str]) -> int:
    nodes: dict[str, set[str]] = {line.split()[0][:-1]: set(line.split()[1:]) for line in puzzle_input}

    def find_outward_paths(from_node: str, target_node: str, previous_path: list[str], cache: dict[str, int]) -> int:
        if from_node not in cache:
            if from_node == target_node:
                cache[from_node] = 1
            else:
                outward_paths: int = 0
                for next_node in nodes[from_node]:
                    if next_node != from_node and next_node not in previous_path:
                        outward_paths += find_outward_paths(next_node, target_node, previous_path + [from_node], cache)
                cache[from_node] = outward_paths
        return cache[from_node]

    return find_outward_paths("you", "out", [], {})

def main() -> tuple[str, int]:
    puzzle_input = util.read.as_lines()

    return "The number of paths leading from you to out is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["aaa: you hhh",
                                       "you: bbb ccc",
                                       "bbb: ddd eee",
                                       "ccc: ddd eee fff",
                                       "ddd: ggg",
                                       "eee: out",
                                       "fff: out",
                                       "ggg: out",
                                       "hhh: ccc fff iii",
                                       "iii: out"]), 5)

if __name__ == "__main__":
    run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3

#Advent of Code
#2025 Day 11, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input: list[str]) -> int:
    nodes: dict[str, set[str]] = {line.split()[0][:-1]: set(line.split()[1:]) for line in puzzle_input}

    def find_outward_paths(from_node: str, target_node: str, avoiding: list[str], previous_path: list[str], cache: dict[str, int]) -> int:
        if from_node not in cache:
            if from_node == target_node:
                cache[from_node] = 1
            else:
                outward_paths: int = 0
                for next_node in nodes[from_node]:
                    if next_node != from_node and next_node not in previous_path and next_node not in avoiding:
                        outward_paths += find_outward_paths(next_node, target_node, avoiding, previous_path + [from_node], cache)
                cache[from_node] = outward_paths
        return cache[from_node]

    svr_to_dac: int = find_outward_paths("svr", "dac", ["fft", "out"], [], {})
    dac_to_fft: int = find_outward_paths("dac", "fft", ["svr", "out"], [], {})
    fft_to_out: int = find_outward_paths("fft", "out", ["svr", "dac"], [], {})

    svr_to_fft: int = find_outward_paths("svr", "fft", ["dac", "out"], [], {})
    fft_to_dac: int = find_outward_paths("fft", "dac", ["svr", "out"], [], {})
    dac_to_out: int = find_outward_paths("dac", "out", ["svr", "dac"], [], {})

    return (svr_to_dac * dac_to_fft * fft_to_out) + (svr_to_fft * fft_to_dac * dac_to_out)

def main() -> tuple[str, int]:
    puzzle_input = util.read.as_lines()

    return "The number of paths leading from svr to out via dac and fft is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["svr: aaa bbb",
                                       "aaa: fft",
                                       "fft: ccc",
                                       "bbb: tty",
                                       "tty: ccc",
                                       "ccc: ddd eee",
                                       "ddd: hub",
                                       "hub: fff",
                                       "eee: dac",
                                       "dac: fff",
                                       "fff: ggg hhh",
                                       "ggg: out",
                                       "hhh: out"]), 2)

if __name__ == "__main__":
    run(main)
Return to solution index