-
Notifications
You must be signed in to change notification settings - Fork 0
/
day22.py
267 lines (233 loc) · 7.85 KB
/
day22.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
from copy import deepcopy
from rich import print
import streamlit as st
import os
from utils import *
import collections
# Define which function to apply to parse the input data, from the text file or the text areas
# file_to_lines, file_to_ints, line_to_ints, line_to_str
basic_transform = file_to_lines
answer = None
st.session_state.file_exists = os.path.exists(get_filename())
with st.sidebar:
select1 = st.selectbox("Part 1", ['examples', 'data'], key='select1')
select2 = st.selectbox("Part 2", ['examples', 'data'], key='select2')
Location = namedtuple('Location', 'x y z')
class PriorityQueue:
def __init__(self):
self.elements = []
def empty(self) -> bool:
return not self.elements
def put(self, item, priority):
heapq.heappush(self.elements, (priority, item))
def get(self):
return heapq.heappop(self.elements)[1]
class Brick:
def __init__(self, start, end, label):
self.s = start
self.e = end
self.label = label
def at_xyz(self, x, y, z):
if x >= self.s.x and x <= self.e.x:
if y >= self.s.y and y <= self.e.y:
if z >= self.s.z and z <= self.e.z:
return True
return False
def extent(self):
x = self.e.x - self.s.x
y = self.e.y - self.s.y
z = self.e.z - self.s.z
return Location(x,y,z)
def __repr__(self):
return self.label
def move(brick, bricks, blocks, is_blocked):
if brick.s.z == 1:
return False
z = brick.s.z - 1
blocked = False
e = brick.extent()
if e.x != 0:
for other in bricks:
if other != brick:
for x in range(brick.s.x, brick.e.x+1):
if other.at_xyz(x, brick.s.y, z):
blocked = True
if other not in blocks:
blocks[other] = set()
blocks[other].add(brick)
if brick not in is_blocked:
is_blocked[brick] = set()
is_blocked[brick].add(other)
elif e.y != 0:
for other in bricks:
if other != brick:
for y in range(brick.s.y, brick.e.y+1):
if other.at_xyz(brick.s.x, y, z):
blocked = True
if other not in blocks:
blocks[other] = set()
blocks[other].add(brick)
if brick not in is_blocked:
is_blocked[brick] = set()
is_blocked[brick].add(other)
else:
for other in bricks:
if other != brick:
if other.at_xyz(brick.s.x, brick.s.y, z):
blocked = True
if other not in blocks:
blocks[other] = set()
blocks[other].add(brick)
if brick not in is_blocked:
is_blocked[brick] = set()
is_blocked[brick].add(other)
if not blocked:
new_s = Location(brick.s.x, brick.s.y, brick.s.z-1)
new_e = Location(brick.e.x, brick.e.y, brick.e.z-1)
brick.s = new_s
brick.e = new_e
return not blocked
def sol1(data):
bricks = []
lowest_z = {}
label_i = 0
for line in data:
s, e = line.split("~")
label = chr(65+label_i)
start = Location(*[int(i) for i in s.split(",")])
end = Location(*[int(i) for i in e.split(",")])
brick = Brick(start, end, label)
bricks.append(brick)
if start.z not in lowest_z:
lowest_z[start.z] = []
lowest_z[start.z].append(brick)
label_i += 1
#st.code((start, end))
# falling
blocks, is_blocked = {}, {}
for z in range(max(list(lowest_z.keys()))+1):
for brick in lowest_z.get(z, []):
moved = move(brick, bricks, blocks, is_blocked)
while moved:
moved = move(brick, bricks, blocks, is_blocked)
total = 0
for brick in bricks:
if brick not in blocks:
total += 1
else:
ok = True
for other in blocks[brick]:
if len(is_blocked[other]) == 1:
ok = False
if ok:
total += 1
return total
def sol2(data):
bricks = []
bricks_d = {}
lowest_z = {}
label_i = 0
for i, line in enumerate(data):
s, e = line.split("~")
label = chr(65+label_i)
start = Location(*[int(i) for i in s.split(",")])
end = Location(*[int(i) for i in e.split(",")])
brick = Brick(start, end, label)
bricks.append(brick)
bricks_d[id(brick)] = brick
if start.z not in lowest_z:
lowest_z[start.z] = []
lowest_z[start.z].append(i)
label_i += 1
# falling
blocks, is_blocked = {}, {}
for z in range(max(list(lowest_z.keys()))+1):
for index in lowest_z.get(z, []):
brick = bricks[index]
moved = move(brick, bricks, blocks, is_blocked)
while moved:
moved = move(brick, bricks, blocks, is_blocked)
# starting from everything is fallen, remove one, and make everything fall
total = 0
for i, brick in enumerate(bricks):
new_bricks = deepcopy(bricks)
new_bricks.pop(i)
lowest_z = {}
for b in new_bricks:
if b.s.z not in lowest_z:
lowest_z[b.s.z] = []
lowest_z[b.s.z].append(b)
for z in range(max(list(lowest_z.keys()))+1):
for b in lowest_z.get(z, []):
moved = move(b, new_bricks, blocks, is_blocked)
if moved:
total += 1
while moved:
moved = move(b, new_bricks, blocks, is_blocked)
return total
if not st.session_state.file_exists:
data = st.text_area("input text from site")
if data:
write_to_file(data)
st.rerun()
st.markdown("This should disappear after execution")
st.divider()
else:
data = load()
if not data:
st.stop()
data = basic_transform(data)
data_bk = data.copy()
st.markdown("### Part 1")
if select1 == 'data':
st.markdown("#### Final answer")
else:
st.markdown("#### Example")
c1, c2 = st.columns(2)
with c1:
value = st.session_state.get("example1_data", "")
data = st.text_area('example 1', value=value)
if data:
st.session_state["example1_data"] = data
data = basic_transform(data)
with c2:
value = st.session_state.get("example1_answer", "")
answer = st.text_input('answer 1', value=value)
if answer:
st.session_state["example1_answer"] = answer
if data:
if answer:
answer = int(answer)
if sol1(data) != answer:
st.markdown(f"**:red[Example failing: {sol1(data)=} != {answer}]**")
else:
st.markdown("**:green[All good]**")
st.markdown(f"{sol1(data)=}")
st.divider()
st.markdown("### Part 2")
answer = None
data = data_bk
if select2 == 'data':
st.markdown("#### Final answer")
else:
st.markdown("#### Example")
c1, c2 = st.columns(2)
with c1:
value = st.session_state.get("example2_data", "")
data = st.text_area('example 2', value=value)
if data:
st.session_state["example2_data"] = data
data = basic_transform(data)
with c2:
value = st.session_state.get("example2_answer", "")
answer = st.text_input('answer 2', value=value)
if answer:
st.session_state["example2_answer"] = answer
if data:
if answer:
answer = int(answer)
if sol2(data) != answer:
st.markdown(f"**:red[Example failing: {sol2(data)=} != {answer}]**")
else:
st.markdown(":green[All good]")
st.markdown(f"{sol2(data)=}")