diff --git a/9/b.py b/9/b.py index b8ae0f6..ca120c2 100644 --- a/9/b.py +++ b/9/b.py @@ -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