added day 09

This commit is contained in:
Peter Hudec
2021-12-09 20:54:05 +01:00
parent cda8b7f385
commit 8936bbfd62
4 changed files with 197 additions and 0 deletions

38
09/solve01.py Normal file
View File

@ -0,0 +1,38 @@
#!/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)