day 10, simple grid walk, refactor into lib
This commit is contained in:
23
d10/run.py
Normal file
23
d10/run.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import sys
|
||||
from grid import *
|
||||
|
||||
L = sys.stdin.read().strip().split("\n")
|
||||
G = [list(map(int,l)) for l in L]
|
||||
|
||||
def walk(grid, x, y, p=-1):
|
||||
v = get_in_grid(G, x, y)
|
||||
if v is None or v != p + 1: # wrong way
|
||||
return
|
||||
if v == 9: # end of trail
|
||||
yield (x, y)
|
||||
for dx, dy in MOVE4:
|
||||
yield from walk(grid, x+dx, y+dy, p+1)
|
||||
|
||||
U = R = 0
|
||||
for (x0,y0) in ((x,y) for y,row in enumerate(G) for x,v in enumerate(row) if v == 0):
|
||||
s = set()
|
||||
for ends in walk(G, x0, y0):
|
||||
s.add(ends)
|
||||
R += 1
|
||||
U += len(s)
|
||||
print(U, R)
|
Reference in New Issue
Block a user