-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday17.py
executable file
·243 lines (174 loc) · 3.99 KB
/
day17.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
#!/usr/bin/env python3
from utils import advent
import re
advent.setup(2018, 17)
fin = advent.get_input()
# print(*fin)
##################################################
rexp = re.compile(r'-?\d+')
minx, miny = 9999, 9999
maxx, maxy = -9999, -9999
horizontal = []
vertical = []
for l in fin:
a, b, c = map(int, rexp.findall(l))
if l.startswith('x'):
minx = min(minx, a)
maxx = max(maxx, a)
miny = min(miny, b)
maxy = max(maxy, c)
vertical.append([a, b, c])
else:
miny = min(miny, a)
maxy = max(maxy, a)
minx = min(minx, b)
maxx = max(maxx, c)
horizontal.append([a, b, c])
# print('Horizontal:')
# for h in horizontal:
# print(h)
# print('Vertical:')
# for v in vertical:
# print(v)
for i in range(len(horizontal)):
horizontal[i][0] -= miny
horizontal[i][1] -= minx
horizontal[i][2] -= minx
for i in range(len(vertical)):
vertical[i][0] -= minx
vertical[i][1] -= miny
vertical[i][2] -= miny
# print('Horizontal:')
# for h in horizontal:
# print(h)
# print('Vertical:')
# for v in vertical:
# print(v)
SAND = 0
WATER = 1
MOVING_WATER = 2
CLAY = 3
gridw = maxx-minx+1
gridh = maxy-miny+1
# print('x', minx, maxx)
# print('y', miny, maxy)
# print('w', gridw)
# print('h', gridh)
grid = []
for _ in range(gridh):
grid.append([SAND] * gridw)
for y, x1, x2 in horizontal:
for x in range(x1, x2+1):
grid[y][x] = CLAY
for x, y1, y2 in vertical:
for y in range(y1, y2+1):
grid[y][x] = CLAY
# Pad borders
gridh += 1
gridw += 2
for i in range(len(grid)):
grid[i] = [SAND] + grid[i] + [SAND]
grid = [[SAND] * gridw] + grid
# with open('viz_initial.txt', 'w') as f:
# for line in grid:
# f.write(''.join(map('.~|#'.__getitem__, line)) + '\n')
def spread(sx, sy):
grid[sy][sx] = WATER
new_sources = set()
if grid[sy+1][sx] == WATER:
should_spread = False
prev = WATER
for cell in grid[sy+1][sx+1:]:
if prev == WATER and cell == CLAY:
should_spread = True
break
elif prev != WATER:
break
prev = cell
if not should_spread:
return
should_spread = False
prev = WATER
for cell in grid[sy+1][sx-1::-1]:
if prev == WATER and cell == CLAY:
should_spread = True
break
elif prev != WATER:
break
prev = cell
if not should_spread:
return
x = sx + 1
while x < gridw and grid[sy][x] == SAND and grid[sy+1][x] != SAND:
grid[sy][x] = WATER
x += 1
if x < gridw and grid[sy+1][x] == SAND:
new_sources.add((x, sy))
x = sx - 1
while x >= 0 and grid[sy][x] == SAND and grid[sy+1][x] != SAND:
grid[sy][x] = WATER
x -= 1
if x >= 0 and grid[sy+1][x] == SAND:
new_sources.add((x, sy))
return new_sources
def fill(sx, sy, recdepth=0):
# if recdepth == 3:
# return
y = sy
while y < gridh and grid[y][sx] == SAND:
y += 1
if y >= gridh:
for y in range(sy, gridh):
grid[y][sx] = WATER
return
y -= 1
for yy in range(y, sy-1, -1):
new_sources = spread(sx, yy)
if new_sources:
for s in new_sources:
fill(*s, recdepth+1)
fill(500 - minx + 1, 0)
# try:
# fill(500 - minx + 1, 0)
# except IndexError:
# print('IndexError!!!')
# pass
# with open('viz_filled.txt', 'w') as f:
# for line in grid:
# f.write(''.join(map(' ~|#'.__getitem__, line)) + '\n')
filled = 0
for row in grid[1:]:
for cell in row:
if cell == WATER:
filled += 1
advent.submit_answer(1, filled)
# Assuming that a situation like this one will never happen:
# #
# #~~~X#
# #~~#X#
# ####X#
# X#
retained = filled
for row in grid:
prev = SAND
for x, cell in enumerate(row):
if cell == WATER and prev in (SAND, MOVING_WATER):
retained -= 1
row[x] = MOVING_WATER
prev = row[x]
prev = SAND
for x, cell in enumerate(row[::-1]):
if cell == WATER and prev in (SAND, MOVING_WATER):
retained -= 1
row[gridw - 1 - x] = MOVING_WATER
prev = row[gridw - 1 - x]
# with open('viz_filled.txt', 'w') as f:
# for line in grid:
# f.write(''.join(map(' ~|#'.__getitem__, line)) + '\n')
filled = 0
for row in grid[1:]:
for cell in row:
if cell == WATER:
filled += 1
# 25447 too low
advent.submit_answer(2, filled)