-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.py
290 lines (253 loc) · 23.8 KB
/
day12.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import marimo
__generated_with = "0.9.26"
app = marimo.App(width="medium")
@app.cell
def __():
data = """SSSSSOOOOOBFFJJFTFBBBBBBBBPPPPPPPPBBBBBBBBBBBSSDDDDDDCCCCCCCCCCCUUUUUAAAAAAAAAAAAAAAUUUUUUUUUUUUUUUUUUUUUUDDDDDDMMMMMZZFFZZFFFFFFFFFFFFFFFII
SSSOOOOOFFFFFFJFFFFBBBBBBBPPPPPPPPPBBBBBBBBBSSSSSSSSDDCCCCCCCCCUUUUUUUUAAAAAAAAAAAAAUUUUUUUUUUUUUUUUUUUUUUUDDDDDMMMMZZZZFZZFFFFFFFFFFFFFFFII
OOSOOOOOFFFFFFJFFFFBBBBBBPPPPPPPPPPBBBBBBBBBBSSSSSSSDC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IIIIIYYYYYYYYYYYYCCYYYYDDDDD
CCCCCCCCCCCCGTTGGGGGGGGGKKEEEEEEEEEEEPPPPPPRUUOOOOOOOOOOSOSSSOOOOOOOOOOOOOIICCISSSSSIIIIIIIIICICCCCCCCCCCCCJJJXXIIIIIIIYYYYYYYYYCCCYYYDDDDDD
CCCCCCCCCCCGGGGGGGGGGGGGKKEEEEEEEEEEEEEPPPPRUUOZOOOOOOOOSOSSSOOOOOOOOOOOOOOIIIISSSUSIIIIIIIIIIICCCCCCCCCCCJJJJJJIIIIIIIYYYYYYYYYCCYYYYYDDDDD
CCCCCCCCCCCCGGGGGGGGGGGGKKKEEEEEBEEEEPPPPPPPPOOOOOOOOOOOOOOUOOOOOOOOOOOOOOOOIIIIVVIIIIIIIIIIIIIIICICCCCCCCCJJYYJYIIIIIIYYYYYYYYYYYYYYYDDDDDD
CCCCCCCCCCCCGGGGGGGGGGGGGKKKEEEEEEEEEPPPPPPPPOOOOOOOOOOOOOOUOOOOOOOOOOOOOOOOOOIIIIIIIIIIIIIIIIIIIIICCCCCCCCYYYYYYIIIIIIIYYYYYYYYYYYYYDDDDDDD
CCCCCCCCCCCCGGGGGGGGGGGGGKKKKKKKEEEECPPPPPPPOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOIIIIIIIIIIIIIIIIIIIIICIICCCCCYYYYYYYYYYYYYYYYYYYYYYYYYYYDDDDDD
CCCCCCCCCCCGGGGGGGGGGGGGGKKKKKKKEEECCCCPPPPIOOOOOOOOOOOOOOPOOOOOOOOOOOOOOOOOGGGGIITIIIIIIIIIIIIIIIIIIICCCYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYDDY"""
data = [list(d) for d in data.splitlines()]
m, n = len(data), len(data[0])
def count_regions():
dirs = ((-1, 0), (0, 1), (1, 0), (0, -1))
seen = set()
# returns area, perimeter
def dfs(i, j, prev):
if (i < 0
or j < 0
or i >= m
or j >= n
):
return 0, 1
if prev != data[i][j]:
return 0, 1
if (i, j) in seen:
return 0, 0
seen.add((i, j))
area, perimeter = 1, 0
for i_off, j_off in dirs:
dir_area, dir_perimeter = dfs(i + i_off, j + j_off, data[i][j])
area += dir_area
perimeter += dir_perimeter
return area, perimeter
res = 0
for i in range(m):
for j in range(n):
area, perimeter = dfs(i, j, data[i][j])
res += area * perimeter
return res
# 1434856
print(count_regions())
return count_regions, data, m, n
@app.cell
def __(data, m, n):
from collections import Counter
def count_regions_with_sides():
dirs = ((-1, 0), (0, 1), (1, 0), (0, -1))
corners = ((-0.5, -0.5), (-0.5, 0.5), (0.5, 0.5), (0.5, -0.5))
seen = set()
def is_in_bounds(i, j):
return i >= 0 and j >= 0 and i < m and j < n
def rotate_right(x, y):
return y, -x
def rotate_left(x, y):
return -y, x
def vsum(v1, v2):
return v1[0] + v2[0], v1[1] + v2[1]
def halfscale(v):
return 0.5*v[0], 0.5*v[1]
# returns area
corner_tracker = Counter()
def dfs(i, j, prev_letter):
if not is_in_bounds(i, j):
return 0
if prev_letter != data[i][j]:
return 0
if (i, j) in seen:
return 0
seen.add((i, j))
# add 4 corners
for corner_i, corner_j in corners:
corner_tracker[(i + corner_i, j + corner_j)] += 1
for direc in dirs:
perp = rotate_right(*direc)
diag = vsum(direc, perp)
d1 = vsum((i, j), direc)
d2 = vsum((i, j), perp)
d3 = vsum((i, j), diag)
if (is_in_bounds(*d1) and is_in_bounds(*d2)
and data[d1[0]][d1[1]] == data[d2[0]][d2[1]] == data[i][j]
and data[d3[0]][d3[1]] != data[i][j]
):
corner_tracker[vsum((i, j), halfscale(diag))] += 2
area = 1
c = 0
for direc in dirs:
next_i, next_j = vsum((i, j), direc)
if (is_in_bounds(next_i, next_j)
and data[i][j] == data[next_i][next_j]
):
# remove corners if necessary
corner1 = halfscale(vsum(direc, rotate_left(*direc)))
corner_tracker[(i + corner1[0], j + corner1[1])] -= 1
corner2 = halfscale(vsum(direc, rotate_right(*direc)))
corner_tracker[(i + corner2[0], j + corner2[1])] -= 1
dir_area = dfs(next_i, next_j, data[i][j])
area += dir_area
return area
res = 0
for i in range(m):
for j in range(n):
area = dfs(i, j, data[i][j])
# num sides == num corners
res += area * sum((+corner_tracker).values())
corner_tracker = Counter()
return res
# 891106
print(count_regions_with_sides())
return Counter, count_regions_with_sides
@app.cell
def __():
return
@app.cell
def __():
return
if __name__ == "__main__":
app.run()