aboutsummaryrefslogtreecommitdiff
path: root/2025/07/puzzle-2.py
blob: 36958899e2624ba8ebc3e8a459dcf5193795768b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/python3

import collections
import functools


Pos = collections.namedtuple('Pos', ['x', 'y'])


def main() -> None:
	with open('input', 'r') as f:
		grid = tuple(tuple(x.strip()) for x in f.readlines())

	pos = Pos(grid[0].index('S'), 0)
	print(npaths(grid, pos))


@functools.cache
def npaths(grid: list[list[str]], pos: Pos) -> int:
	if pos.y == len(grid):
		return 1
	if grid[pos.y][pos.x] == '^':
		return (
			  npaths(grid, Pos(pos.x - 1, pos.y))
			+ npaths(grid, Pos(pos.x + 1, pos.y))
		)
	return npaths(grid, Pos(pos.x, pos.y + 1))


if __name__ == '__main__':
	main()