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 width, height = x, y
lows = [] lows = []
candidates = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for i, ((x, y), value) in enumerate(zone.items()): for i, ((x, y), value) in enumerate(zone.items()):
if x > 0 and zone[(x-1, y)] <= value: ok = True
continue for c_x, c_y in candidates:
if x < width-1 and zone[(x+1, y)] <= value: newx = x + c_x
continue newy = y + c_y
if y > 0 and zone[(x, y-1)] <= value: if newx < 0 or newx >= width or newy < 0 or newy >= height:
continue continue # off map
if y < height-1 and zone[(x, y+1)] <= value: newpoint = (newx, newy, )
continue if zone[newpoint] <= value:
lows.append((x, y, )) ok = False
break
if ok:
lows.append((x, y, ))
def find_basin(point, in_basin): def find_basin(point, in_basin):
# from the starting point, explore points around it recursing into each # from the starting point, explore points around it recursing into each
in_basin.update([point]) in_basin.update([point])
candidates = [(-1, 0), (1, 0), (0, -1), (0,1)]
for c_x, c_y in candidates: for c_x, c_y in candidates:
newx = point[0] + c_x newx = point[0] + c_x
newy = point[1] + c_y newy = point[1] + c_y