From d71ee57806c69b40744f4e0191986150b5ae0bd2 Mon Sep 17 00:00:00 2001 From: dave Date: Wed, 8 Dec 2021 21:28:03 -0800 Subject: [PATCH] minor 9b refactor --- 9/b.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) 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