feat: add solution 2025/07
This commit is contained in:
16
data/example/2025/07.txt
Normal file
16
data/example/2025/07.txt
Normal file
@@ -0,0 +1,16 @@
|
||||
.......S.......
|
||||
...............
|
||||
.......^.......
|
||||
...............
|
||||
......^.^......
|
||||
...............
|
||||
.....^.^.^.....
|
||||
...............
|
||||
....^.^...^....
|
||||
...............
|
||||
...^.^...^.^...
|
||||
...............
|
||||
..^...^.....^..
|
||||
...............
|
||||
.^.^.^.^.^...^.
|
||||
...............
|
||||
56
src/solution/year_2025/day_07.py
Normal file
56
src/solution/year_2025/day_07.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from functools import lru_cache
|
||||
from typing import Any
|
||||
|
||||
|
||||
def part_1(input_data: str) -> Any:
|
||||
lines = input_data.splitlines()
|
||||
first, rest = lines[0], lines[1:]
|
||||
|
||||
s = {i for i, x in enumerate(first) if x == "S"}
|
||||
ns: set[int] = set()
|
||||
|
||||
c = 0
|
||||
for line in rest:
|
||||
ns.clear()
|
||||
|
||||
while s:
|
||||
n = s.pop()
|
||||
if line[n] == "^":
|
||||
ns.add(n - 1)
|
||||
ns.add(n + 1)
|
||||
c += 1
|
||||
else:
|
||||
ns.add(n)
|
||||
|
||||
s.update(ns)
|
||||
|
||||
return c
|
||||
|
||||
|
||||
def trace_path(n: int, i: int, lines: list[str]) -> int:
|
||||
@lru_cache(None)
|
||||
def aux(n: int, i: int) -> int:
|
||||
for idx, line in filter(lambda x: x[0] >= i, enumerate(lines)):
|
||||
if line[n] == "^":
|
||||
left = aux(n - 1, idx + 1)
|
||||
right = aux(n + 1, idx + 1)
|
||||
return 1 + left + right
|
||||
|
||||
return 0
|
||||
|
||||
return aux(n, i)
|
||||
|
||||
|
||||
def part_2(input_data: str) -> Any:
|
||||
lines = input_data.splitlines()
|
||||
first, rest = lines[0], lines[1:]
|
||||
n = next(filter(lambda x: x[1] == "S", enumerate(first)))[0]
|
||||
return 1 + trace_path(n, 0, rest)
|
||||
|
||||
|
||||
def test_part_1(example_data):
|
||||
assert part_1(example_data) == 21
|
||||
|
||||
|
||||
def test_part_2(example_data):
|
||||
assert part_2(example_data) == 40
|
||||
Reference in New Issue
Block a user