From bea4dab0ffc5856586c9b583043690d02c4e79a2 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 14 Dec 2022 15:25:27 +0100 Subject: day 14 part 2 disappointingly easy --- 22.14/main.py | 55 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/22.14/main.py b/22.14/main.py index 5078f4d..e2545bb 100644 --- a/22.14/main.py +++ b/22.14/main.py @@ -13,26 +13,39 @@ for line in stdin: solid.add((x, y)) max_y = max([y for x,y in solid]) -print(max_y) -def drop(x, y): - while True: - while not (x, y+1) in solid: - y += 1 - if max_y < y: - return False - if not (x-1, y+1) in solid: - x -= 1 - y += 1 - elif not (x+1, y+1) in solid: - x += 1 - y += 1 - else: - print(x, y) - solid.add((x, y)) - return True +def partOne(s): + def drop(x, y): + while True: + while not (x, y+1) in s: + y += 1 + if max_y < y: + return False + if not (x-1, y+1) in s: + x -= 1 + y += 1 + elif not (x+1, y+1) in s: + x += 1 + y += 1 + else: + s.add((x, y)) + return True + n = 0 + while drop(500, 0): + n += 1 + return n -n = 0 -while drop(500, 0): - n += 1 -print(n) +def partTwo(s): + total = 1 + last = {500} + for y in range(1, max_y + 2): + cur = last.copy() + cur = cur.union(map(lambda x: x-1, last)) + cur = cur.union(map(lambda x: x+1, last)) + cur = set(filter(lambda x: not (x, y) in s, cur)) + last = cur + total += len(cur) + return total + +print(partOne(solid.copy())) +print(partTwo(solid.copy())) -- cgit 1.4.1-2-gfad0