minor 9b refactor
This commit is contained in:
parent
5381a654c1
commit
d71ee57806
25
9/b.py
25
9/b.py
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue