From efd7f887b8b435057d0e4db82bb984fde0672174 Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Fri, 22 Dec 2023 06:47:19 +0000 Subject: [PATCH] day 22 part 2 --- day22/sol.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/day22/sol.py b/day22/sol.py index afc8412..9e64ac6 100644 --- a/day22/sol.py +++ b/day22/sol.py @@ -38,7 +38,8 @@ def solve(f): check(data) settle(data) check(data) - disintegrate(data) + #disintegrate(data) + cascade(data) def settle(data): def zindex(b): @@ -118,6 +119,43 @@ def disintegrate(data): return (t) +def cascade(data): + print(*data, sep="\n") + t = 0 + + #for i,x in enumerate(data): + # assert not canlower(x,data[:i]) + + for i,x in enumerate(data): + below = data[:i] + upper = data[i+1:] + moved = 0 + + if below: + top = max(z[1] for x,y,z in below) + 1 + else: + top = 1 + for b in upper: + # lower blocks one at a time + z = b[2][0] + if z > top: + b = lower(b, z-top) + + while canlower(b, below): + b = lower(b) + + if b[2][0] < z: + moved += 1 + + #print("lowering block %d by %d" % (i,n)) + below.append(b) + top = max(top, b[2][1]+1) + + print(i, x, "caused %d blocks to move" % moved) + t += moved + + print(t) + return (t) def lower(brick, n=1): """lower a brick n spaces in the z direction"""