adventofcode-2021/09/solve01.py
2021-12-09 20:54:05 +01:00

38 lines
812 B
Python

#!/usr/bin/env python
def get_neighour(heightmap, x, y, deltax, deltay):
if x+deltax < 0:
return 10
if y+deltay < 0:
return 10
try:
return heightmap[y+deltay][x+deltax]
except IndexError:
return 10
def check_low_point(heightmap, x, y):
xy = heightmap[y][x]
if get_neighour(heightmap,x,y, 1,0) <= xy:
return 0
if get_neighour(heightmap,x,y, -1,0) <= xy:
return 0
if get_neighour(heightmap,x,y, 0,1) <= xy:
return 0
if get_neighour(heightmap,x,y, 0,-1) <= xy:
return 0
#print("found basin at %d:%d" % (x, y))
return xy+1
heightmap = []
with open("input01.txt","r") as f:
for line in f:
heightmap.append([int(x) for x in list(line.strip())])
result = 0
for y in range(len(heightmap)):
for x in range(len(heightmap[y])):
result += check_low_point(heightmap, x,y)
print(result)