summaryrefslogtreecommitdiff
path: root/22_2023.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-06-15 22:20:32 -0600
committerJack Sangdahl <[email protected]>2024-06-15 22:20:32 -0600
commit8e313e2614c7a0fe5af924febdbacc650e33835e (patch)
tree63426b6942ceebe17708140c972a7d7451bd2f74 /22_2023.py
downloadaoc-8e313e2614c7a0fe5af924febdbacc650e33835e.tar.gz
aoc-8e313e2614c7a0fe5af924febdbacc650e33835e.zip
intial
Diffstat (limited to '22_2023.py')
-rw-r--r--22_2023.py102
1 files changed, 102 insertions, 0 deletions
diff --git a/22_2023.py b/22_2023.py
new file mode 100644
index 0000000..389f62f
--- /dev/null
+++ b/22_2023.py
@@ -0,0 +1,102 @@
+import sys
+import re
+from copy import deepcopy
+from math import gcd
+from collections import defaultdict, Counter, deque
+import heapq
+import math
+
+D = open(sys.argv[1]).read().strip()
+L = D.split("\n")
+G = [[c for c in row] for row in L]
+R = len(G)
+C = len(G[0])
+
+BS = []
+for line in L:
+ st, ed = line.split("~")
+ sx, sy, sz = [int(x) for x in st.split(",")]
+ ex, ey, ez = [int(x) for x in ed.split(",")]
+ B = []
+ if sx == ex and sy == ey:
+ assert sz <= ez
+ for z in range(sz, ez + 1):
+ B.append((sx, sy, z))
+ elif sx == ex and sz == ez:
+ assert sy <= ey
+ for y in range(sy, ey + 1):
+ B.append((sx, y, sz))
+ elif sy == ey and sz == ez:
+ assert sx <= ex
+ for x in range(sx, ex + 1):
+ B.append((x, sy, sz))
+ else:
+ assert False
+ assert len(B) >= 1
+ BS.append(B)
+
+SEEN = set()
+for B in BS:
+ for x, y, z in B:
+ SEEN.add((x, y, z))
+
+while True:
+ any_ = False
+ for i, B in enumerate(BS):
+ ok = True
+ for x, y, z in B:
+ if z == 1:
+ ok = False
+ if (x, y, z - 1) in SEEN and (x, y, z - 1) not in B:
+ ok = False
+ if ok:
+ any_ = True
+ for x, y, z in B:
+ assert (x, y, z) in SEEN
+ SEEN.discard((x, y, z))
+ SEEN.add((x, y, z - 1))
+ BS[i] = [(x, y, z - 1) for (x, y, z) in B]
+ if not any_:
+ break
+OLD_SEEN = deepcopy(SEEN)
+OLD_B = deepcopy(BS)
+
+p1 = 0
+p2 = 0
+for i, B in enumerate(BS):
+ SEEN = deepcopy(OLD_SEEN)
+ BS = deepcopy(OLD_B)
+ for C in BS:
+ for x, y, z in C:
+ assert (x, y, z) in SEEN
+
+ for x, y, z in B:
+ SEEN.discard((x, y, z))
+
+ FALL = set()
+ while True:
+ any_ = False
+ for j, C in enumerate(BS):
+ if j == i:
+ continue
+ ok = True
+ for x, y, z in C:
+ if z == 1:
+ ok = False
+ if (x, y, z - 1) in SEEN and (x, y, z - 1) not in C:
+ ok = False
+ if ok:
+ FALL.add(j)
+ for x, y, z in C:
+ assert (x, y, z) in SEEN
+ SEEN.discard((x, y, z))
+ SEEN.add((x, y, z - 1))
+ BS[j] = [(x, y, z - 1) for (x, y, z) in C]
+ any_ = True
+ if not any_:
+ break
+ if len(FALL) == 0:
+ p1 += 1
+ p2 += len(FALL)
+print(p1)
+print(p2)