Skip to content

Commit 9abc0fb

Browse files
committed
AoC 2025 Day 7 - cleanup
1 parent 842ba3f commit 9abc0fb

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/main/python/AoC2025_07.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Advent of Code 2025 Day 7
44
#
55

6+
import itertools
67
import sys
78
from functools import cache
89

@@ -13,7 +14,7 @@
1314
from aoc.grid import Cell
1415
from aoc.grid import CharGrid
1516

16-
Input = InputData
17+
Input = CharGrid
1718
Output1 = int
1819
Output2 = int
1920

@@ -37,41 +38,44 @@
3738
...............
3839
"""
3940

41+
SPLITTER = "^"
42+
START = "S"
43+
4044

4145
class Solution(SolutionBase[Input, Output1, Output2]):
4246
def parse_input(self, input_data: InputData) -> Input:
43-
return input_data
47+
return CharGrid.from_strings(list(input_data))
4448

45-
def part_1(self, inputs: Input) -> Output1:
46-
grid = CharGrid.from_strings(list(inputs))
47-
beams = {next(grid.get_all_equal_to("S")).col}
49+
def part_1(self, grid: Input) -> Output1:
50+
beams = {next(grid.get_all_equal_to(START)).col}
4851
ans = 0
49-
for sp in grid.get_all_equal_to("^"):
52+
for sp in grid.get_all_equal_to(SPLITTER):
5053
if sp.col in beams:
5154
ans += 1
5255
beams.remove(sp.col)
5356
for d in (Direction.LEFT, Direction.RIGHT):
5457
beams.add(sp.at(d).col)
5558
return ans
5659

57-
def part_2(self, inputs: Input) -> Output2:
58-
grid = CharGrid.from_strings(list(inputs))
59-
start = next(grid.get_all_equal_to("^"))
60-
splitters = set(grid.get_all_equal_to("^")) | {start}
60+
def part_2(self, grid: Input) -> Output2:
61+
start = next(grid.get_all_equal_to(SPLITTER))
62+
splitters = {start} | set(grid.get_all_equal_to(SPLITTER))
6163

6264
@cache
6365
def dfs(cell: Cell) -> int:
6466
if cell == start:
6567
return 1
66-
ans = 0
67-
for n in grid.get_cells_n(cell):
68-
if n in splitters:
69-
break
70-
for d in (Direction.LEFT, Direction.RIGHT):
71-
nxt = n.at(d)
72-
if nxt in splitters:
73-
ans += dfs(nxt)
74-
return ans
68+
return sum(
69+
dfs(nxt)
70+
for nxt in (
71+
n.at(d)
72+
for d in (Direction.LEFT, Direction.RIGHT)
73+
for n in itertools.takewhile(
74+
lambda n: n not in splitters, grid.get_cells_n(cell)
75+
)
76+
)
77+
if nxt in splitters
78+
)
7579

7680
bottom_left = Cell(grid.get_max_row_index(), 0)
7781
return dfs(bottom_left) + sum(

0 commit comments

Comments
 (0)