aoc2021/d09/d09_2.py

26 lines
532 B
Python

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)))
def neigh(x,y,W:set):
p = L[x][y]
if p<9:
W.add((x,y))
for (i,j) in [(0,1),(1,0),(-1,0),(0,-1)]:
if L[x+i][y+j]>p and (x+i,y+j) not in W:
neigh(x+i,y+j,W)
#printgrid()
S = []
for x in range(1,H-1):
for y in range(1,W-1):
V = set()
neigh(x,y,V)
S.append(len(V))
#print(V)
#print(S)
print(sorted(S)[-3:])