aoc2021/d09/d09_2.py

26 lines
532 B
Python
Raw Normal View History

2021-12-09 22:55:58 +00:00
import sys
L = list(map(lambda s: [9]+list(map(int, s))+[9] ,sys.stdin.read().splitlines()))
W = len(L[0])
L = [[9]*W] + L + [[9]*W]
H = len(L)
def printgrid():
for row in L:
print("".join(map(str,row)))
2021-12-10 00:10:15 +00:00
def neigh(x,y,W:set):
2021-12-09 22:55:58 +00:00
p = L[x][y]
if p<9:
2021-12-10 00:10:15 +00:00
W.add((x,y))
2021-12-09 22:55:58 +00:00
for (i,j) in [(0,1),(1,0),(-1,0),(0,-1)]:
2021-12-10 00:10:15 +00:00
if L[x+i][y+j]>p and (x+i,y+j) not in W:
neigh(x+i,y+j,W)
#printgrid()
2021-12-09 22:55:58 +00:00
S = []
for x in range(1,H-1):
for y in range(1,W-1):
2021-12-10 00:10:15 +00:00
V = set()
neigh(x,y,V)
S.append(len(V))
#print(V)
#print(S)
print(sorted(S)[-3:])