diff options
author | dzwdz | 2022-12-14 15:25:27 +0100 |
---|---|---|
committer | dzwdz | 2022-12-14 15:25:27 +0100 |
commit | bea4dab0ffc5856586c9b583043690d02c4e79a2 (patch) | |
tree | 39544ab180f915c41ed98815cac1b69391fdfc4d /22.14 | |
parent | 8c1c438930eb450102f992fbb88d777c9476cbf4 (diff) |
day 14 part 2
disappointingly easy
Diffstat (limited to '22.14')
-rw-r--r-- | 22.14/main.py | 55 |
1 files 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())) |