Skip to content

Commit 10b898c

Browse files
committed
Add solution to 2023-12-22
1 parent a23e24a commit 10b898c

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

2023/day22/solutions.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import re
2+
3+
with open("input") as f:
4+
ls = f.read().strip().split()
5+
6+
ns = (map(int, re.findall("\d+", l)) for l in ls)
7+
bricks = []
8+
for x1, y1, z1, x2, y2, z2 in ns:
9+
bricks.append(
10+
{
11+
(x, y, z)
12+
for x in range(x1, x2 + 1)
13+
for y in range(y1, y2 + 1)
14+
for z in range(z1, z2 + 1)
15+
}
16+
)
17+
18+
all_locs = set.union(*bricks)
19+
while True:
20+
any_dropped = False
21+
for i, brick in enumerate(bricks):
22+
all_without = all_locs - brick
23+
if all((x, y, z - 1) not in all_without for (x, y, z) in brick) and all(z > 1 for _, _, z in brick):
24+
brick = {(x, y, z - 1) for (x, y, z) in brick}
25+
bricks[i] = brick
26+
all_locs = all_without | brick
27+
any_dropped = True
28+
if not any_dropped:
29+
break
30+
31+
depends_on = [
32+
{
33+
j
34+
for j, brick2 in enumerate(bricks)
35+
if i != j and any((x, y, z - 1) in brick2 for (x, y, z) in brick1)
36+
}
37+
for i, brick1 in enumerate(bricks)
38+
]
39+
40+
# Part 1
41+
print(len(bricks) - len({list(v)[0] for v in depends_on if len(v) == 1}))
42+
43+
# Part 2
44+
on_floor = {k for k, v in enumerate(depends_on) if not v}
45+
46+
47+
def num_fall_if_removed(removed):
48+
unsupported = {
49+
k
50+
for k, v in enumerate(depends_on)
51+
if not v - removed and k not in removed | on_floor
52+
}
53+
if not unsupported:
54+
return 0
55+
return len(unsupported) + num_fall_if_removed(removed | unsupported)
56+
57+
58+
print(sum(num_fall_if_removed({i}) for i in range(len(bricks))))

0 commit comments

Comments
 (0)