2025 - Day 7 (Laboratories)

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
49
50
51
52
#!/usr/bin/env python3

#Advent of Code
#2025 Day 7, 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:
    beams: set[int] = {puzzle_input[0].find("S")}
    splits: int = 0
    for row in puzzle_input[1:]:
        new_beams: set[int] = set()
        for beam in beams:
            if row[beam] == "^":
                new_beams.add(beam - 1)
                new_beams.add(beam + 1)
                splits += 1
            else:
                new_beams.add(beam)
        beams = new_beams
    return splits

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

    return "The number of times that the beam will split is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve([".......S.......",
                                       "...............",
                                       ".......^.......",
                                       "...............",
                                       "......^.^......",
                                       "...............",
                                       ".....^.^.^.....",
                                       "...............",
                                       "....^.^...^....",
                                       "...............",
                                       "...^.^...^.^...",
                                       "...............",
                                       "..^...^.....^..",
                                       "...............",
                                       ".^.^.^.^.^...^.",
                                       "..............."]), 21)

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
#!/usr/bin/env python3

#Advent of Code
#2025 Day 7, 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

from collections import defaultdict

def solve(puzzle_input: list[str]) -> int:
    beams: defaultdict[int, int] = defaultdict(int)
    beams[puzzle_input[0].find("S")] = 1
    for row in puzzle_input[1:]:
        new_beams: defaultdict[int, int] = defaultdict(int)
        for beam in beams:
            if row[beam] == "^":
                new_beams[beam - 1] += beams[beam]
                new_beams[beam + 1] += beams[beam]
            else:
                new_beams[beam] += beams[beam]
        beams = new_beams
    return sum(beams.values())

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

    return "The number of times that the beam will split is {}.", solve(puzzle_input)

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve([".......S.......",
                                       "...............",
                                       ".......^.......",
                                       "...............",
                                       "......^.^......",
                                       "...............",
                                       ".....^.^.^.....",
                                       "...............",
                                       "....^.^...^....",
                                       "...............",
                                       "...^.^...^.^...",
                                       "...............",
                                       "..^...^.....^..",
                                       "...............",
                                       ".^.^.^.^.^...^.",
                                       "..............."]), 40)

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