-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday22.py
More file actions
77 lines (54 loc) · 1.96 KB
/
day22.py
File metadata and controls
77 lines (54 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# My original implementation was very slow
# This is an adaptation of the solution from HyperNeutrino
from collections import deque
def xy_overlap(a, b):
return max(a[0], b[0]) <= min(a[3], b[3]) and max(a[1], b[1]) <= min(a[4], b[4])
def settle(bricks):
bricks.sort(key=lambda x: x[2])
for i, brick in enumerate(bricks):
max_z = 1
for below in bricks[:i]:
if xy_overlap(brick, below):
max_z = max(max_z, below[5] + 1)
brick[5] -= brick[2] - max_z
brick[2] = max_z
bricks.sort(key=lambda x: x[2])
def build_graph(bricks):
supports = {i: set() for i in range(len(bricks))}
supported_by = {i: set() for i in range(len(bricks))}
for j, higher in enumerate(bricks):
for i, lower in enumerate(bricks[:j]):
if xy_overlap(lower, higher) and higher[2] == lower[5] + 1:
supports[i].add(j)
supported_by[j].add(i)
return supports, supported_by
def part1(input):
bricks = [
list(map(int, line.replace("~", ",").split(","))) for line in input.splitlines()
]
settle(bricks)
supports, supported_by = build_graph(bricks)
answer = 0
for i in range(len(bricks)):
if all(len(supported_by[j]) > 1 for j in supports[i]):
answer += 1
return answer
def part2(input):
bricks = [
list(map(int, line.replace("~", ",").split(","))) for line in input.splitlines()
]
settle(bricks)
supports, supported_by = build_graph(bricks)
answer = 0
for i in range(len(bricks)):
queue = deque([j for j in supports[i] if len(supported_by[j]) == 1])
falling = set(queue)
falling.add(i)
while queue:
j = queue.popleft()
for k in supports[j].difference(falling):
if supported_by[k].issubset(falling):
queue.append(k)
falling.add(k)
answer += len(falling) - 1
return answer