From 79befca8693af9e6e43940a06c17521b522210c4 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Mon, 17 Dec 2018 20:26:47 +1030 Subject: [PATCH] 2018 Day 17 not entirely garbage edition --- 2018/day17.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/2018/day17.py b/2018/day17.py index 5571a1b..ec08ee5 100644 --- a/2018/day17.py +++ b/2018/day17.py @@ -27,17 +27,19 @@ for xmin, xmax, ymin, ymax in zip(Xmin, Xmax, Ymin, Ymax): grid = init_grid.copy() +drop_point_blacklist = set() def drop_water(source=spring, skip_cliffs=set()): - # TODO: The exit condition on this is borked and it will probably run forever. Killing it after like a minute and then running the things at the end gives the right answers. - # I'll probably revisit this properly later + if tuple(source) in drop_point_blacklist: + return False x, y = source # print('Dropping water from', source) surface = (grid[x, y:] > 0).argmax() if surface == 0: if (grid[x, y-1:] > 0).argmax() == 0: grid[x, y:] = -1 + drop_point_blacklist.add(tuple(source)) return False # raise ValueError('Out of bounds') surface_y = surface + y @@ -77,6 +79,10 @@ def drop_water(source=spring, skip_cliffs=set()): if cliff and (cliff not in skip_cliffs): cliffs.add((x + cliff, surface_y)) + if cliffs and len([c for c in cliffs if tuple(c) in drop_point_blacklist]) == len(cliffs): + drop_point_blacklist.add(tuple(source)) + return False + succeeded = False while sum([drop_water(c) for c in cliffs]): succeeded = True