|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# |
| 3 | +# Advent of Code 2025 Day 7 |
| 4 | +# |
| 5 | + |
| 6 | +import sys |
| 7 | +from functools import cache |
| 8 | + |
| 9 | +from aoc.common import InputData |
| 10 | +from aoc.common import SolutionBase |
| 11 | +from aoc.common import aoc_samples |
| 12 | +from aoc.geometry import Direction |
| 13 | +from aoc.grid import Cell |
| 14 | +from aoc.grid import CharGrid |
| 15 | + |
| 16 | +Input = InputData |
| 17 | +Output1 = int |
| 18 | +Output2 = int |
| 19 | + |
| 20 | + |
| 21 | +TEST = """\ |
| 22 | +.......S....... |
| 23 | +............... |
| 24 | +.......^....... |
| 25 | +............... |
| 26 | +......^.^...... |
| 27 | +............... |
| 28 | +.....^.^.^..... |
| 29 | +............... |
| 30 | +....^.^...^.... |
| 31 | +............... |
| 32 | +...^.^...^.^... |
| 33 | +............... |
| 34 | +..^...^.....^.. |
| 35 | +............... |
| 36 | +.^.^.^.^.^...^. |
| 37 | +............... |
| 38 | +""" |
| 39 | + |
| 40 | + |
| 41 | +class Solution(SolutionBase[Input, Output1, Output2]): |
| 42 | + def parse_input(self, input_data: InputData) -> Input: |
| 43 | + return input_data |
| 44 | + |
| 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} |
| 48 | + ans = 0 |
| 49 | + for sp in grid.get_all_equal_to("^"): |
| 50 | + if sp.col in beams: |
| 51 | + ans += 1 |
| 52 | + beams.remove(sp.col) |
| 53 | + for d in (Direction.LEFT, Direction.RIGHT): |
| 54 | + beams.add(sp.at(d).col) |
| 55 | + return ans |
| 56 | + |
| 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} |
| 61 | + |
| 62 | + @cache |
| 63 | + def dfs(cell: Cell) -> int: |
| 64 | + if cell == start: |
| 65 | + 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 |
| 75 | + |
| 76 | + bottom_left = Cell(grid.get_max_row_index(), 0) |
| 77 | + return dfs(bottom_left) + sum( |
| 78 | + dfs(cell) for cell in grid.get_cells_e(bottom_left) |
| 79 | + ) |
| 80 | + |
| 81 | + @aoc_samples( |
| 82 | + ( |
| 83 | + ("part_1", TEST, 21), |
| 84 | + ("part_2", TEST, 40), |
| 85 | + ) |
| 86 | + ) |
| 87 | + def samples(self) -> None: |
| 88 | + pass |
| 89 | + |
| 90 | + |
| 91 | +solution = Solution(2025, 7) |
| 92 | + |
| 93 | + |
| 94 | +def main() -> None: |
| 95 | + solution.run(sys.argv) |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + main() |
0 commit comments