minor 9b refactor

This commit is contained in:
dave 2021-12-08 21:28:03 -08:00
parent 5381a654c1
commit d71ee57806
1 changed files with 13 additions and 12 deletions

25
9/b.py
View File

@ -12,27 +12,28 @@ with open("input.txt") as f:
width, height = x, y
lows = []
candidates = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for i, ((x, y), value) in enumerate(zone.items()):
if x > 0 and zone[(x-1, y)] <= value:
continue
if x < width-1 and zone[(x+1, y)] <= value:
continue
if y > 0 and zone[(x, y-1)] <= value:
continue
if y < height-1 and zone[(x, y+1)] <= value:
continue
lows.append((x, y, ))
ok = True
for c_x, c_y in candidates:
newx = x + c_x
newy = y + c_y
if newx < 0 or newx >= width or newy < 0 or newy >= height:
continue # off map
newpoint = (newx, newy, )
if zone[newpoint] <= value:
ok = False
break
if ok:
lows.append((x, y, ))
def find_basin(point, in_basin):
# from the starting point, explore points around it recursing into each
in_basin.update([point])
candidates = [(-1, 0), (1, 0), (0, -1), (0,1)]
for c_x, c_y in candidates:
newx = point[0] + c_x
newy = point[1] + c_y