2025: d07: ex2: add solution
This commit is contained in:
parent
bbad71c5f6
commit
78aae83a43
1 changed files with 50 additions and 0 deletions
50
2025/d07/ex2/ex2.py
Executable file
50
2025/d07/ex2/ex2.py
Executable file
|
|
@ -0,0 +1,50 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import functools
|
||||
import sys
|
||||
from typing import NamedTuple
|
||||
|
||||
|
||||
class Point(NamedTuple):
|
||||
x: int
|
||||
y: int
|
||||
|
||||
|
||||
def solve(input: list[str]) -> int:
|
||||
def parse(input: list[str]) -> tuple[Point, set[Point]]:
|
||||
start: Point | None = None
|
||||
splitters: set[Point] = set()
|
||||
for x, line in enumerate(input):
|
||||
for y, c in enumerate(line):
|
||||
pos = Point(x, y)
|
||||
if c == "S":
|
||||
start = pos
|
||||
elif c == "^":
|
||||
splitters.add(pos)
|
||||
assert start is not None
|
||||
return start, splitters
|
||||
|
||||
def count_timelines(start: Point, splitters: set[Point]) -> int:
|
||||
max_x = max(p.x for p in splitters)
|
||||
|
||||
@functools.cache
|
||||
def rec(p: Point) -> int:
|
||||
if p.x > max_x:
|
||||
return 1
|
||||
if p not in splitters:
|
||||
return rec(Point(p.x + 1, p.y))
|
||||
return rec(Point(p.x + 1, p.y - 1)) + rec(Point(p.x + 1, p.y + 1))
|
||||
|
||||
return rec(start)
|
||||
|
||||
start, splitters = parse(input)
|
||||
return count_timelines(start, splitters)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = sys.stdin.read().splitlines()
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue