Skip to content

Commit 2eeeab2

Browse files
committed
AoC 2025 Day 7
1 parent f97c1e2 commit 2eeeab2

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
## 2025
44

5-
![](https://img.shields.io/badge/stars%20⭐-13-yellow)
6-
![](https://img.shields.io/badge/days%20completed-6-red)
5+
![](https://img.shields.io/badge/stars%20⭐-14-yellow)
6+
![](https://img.shields.io/badge/days%20completed-7-red)
77

88
<!-- @BEGIN:ImplementationsTable:2025@ -->
99
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
1010
| ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
11-
| python3 | [](src/main/python/AoC2025_01.py) | [](src/main/python/AoC2025_02.py) | [](src/main/python/AoC2025_03.py) | [](src/main/python/AoC2025_04.py) | [](src/main/python/AoC2025_05.py) | [](src/main/python/AoC2025_06.py) | | | | | | |
11+
| python3 | [](src/main/python/AoC2025_01.py) | [](src/main/python/AoC2025_02.py) | [](src/main/python/AoC2025_03.py) | [](src/main/python/AoC2025_04.py) | [](src/main/python/AoC2025_05.py) | [](src/main/python/AoC2025_06.py) | [](src/main/python/AoC2025_07.py) | | | | | |
1212
| java | [](src/main/java/AoC2025_01.java) | [](src/main/java/AoC2025_02.java) | [](src/main/java/AoC2025_03.java) | [](src/main/java/AoC2025_04.java) | [](src/main/java/AoC2025_05.java) | [](src/main/java/AoC2025_06.java) | | | | | | |
1313
| bash | [](src/main/bash/AoC2025_01.sh) | [](src/main/bash/AoC2025_02.sh) | [](src/main/bash/AoC2025_03.sh) | [](src/main/bash/AoC2025_04.sh) | [](src/main/bash/AoC2025_05.sh) | [](src/main/bash/AoC2025_06.sh) | | | | | | |
1414
<!-- @END:ImplementationsTable:2025@ -->

src/main/python/AoC2025_07.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)