-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday20.py
306 lines (250 loc) · 9.45 KB
/
day20.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
from typing import List, Set
from collections import defaultdict
from math import sqrt
from copy import deepcopy
import numpy as np
def parse_input(filename: str) -> dict:
tile_info = [tile for tile in open(filename).read().split('\n\n')]
tiles = {}
for info in tile_info:
info_rows = [row.strip() for row in info.split('\n')]
tile_number = int((info_rows[0].split(' ')[1][:-1]))
contents = info_rows[1:]
grid = np.zeros((len(contents), len(contents)))
for y, row in enumerate(contents):
for x, ch in enumerate(row):
if ch == '#':
grid[y][x] = 1
tiles[tile_number] = grid
return tiles
def extract_edges(tiles: dict) -> dict:
tile_edges = defaultdict(lambda: [])
for tile in tiles:
grid = tiles[tile]
top = grid[0, :]
bottom = grid[9, :]
left = grid[:, 0]
right = grid[:, 9]
tile_edges[tile].append(int(''.join([str(int(i)) for i in top]), 2))
tile_edges[tile].append(int(''.join([str(int(i)) for i in top[::-1]]), 2))
tile_edges[tile].append(int(''.join([str(int(i)) for i in right]), 2))
tile_edges[tile].append(int(''.join([str(int(i)) for i in right[::-1]]), 2))
tile_edges[tile].append(int(''.join([str(int(i)) for i in bottom]), 2))
tile_edges[tile].append(int(''.join([str(int(i)) for i in bottom[::-1]]), 2))
tile_edges[tile].append(int(''.join([str(int(i)) for i in left]), 2))
tile_edges[tile].append(int(''.join([str(int(i)) for i in left[::-1]]), 2))
return dict(tile_edges)
def find_matches(edges: dict) -> dict:
matches = {}
for current_tile in edges:
tile_matches = set()
for i, edge in enumerate(edges[current_tile]):
for tile in edges:
if tile != current_tile and edge in edges[tile]:
tile_matches.add(tile)
matches[current_tile] = tile_matches
return matches
def all_neighbors_in_match(neighbors: List[int], matches: Set[int]) -> bool:
for neighbor in neighbors:
if neighbor not in matches:
return False
return True
def get_match(neighbors: List[int], sorted_tiles: List[int], matches: dict, visited: List[int]) -> int:
if not neighbors:
return sorted_tiles[0]
else:
for tile in sorted_tiles:
if all_neighbors_in_match(neighbors, matches[tile]):
return tile
return None
def get_neighbors(grid: List[List[int]], y: int, x: int) -> List[int]:
neighbors = []
if x - 1 >= 0 and grid[y][x - 1] != 0:
neighbors.append(grid[y][x - 1])
if x + 1 < len(grid) and grid[y][x + 1] != 0:
neighbors.append(grid[y][x + 1])
if y - 1 >= 0 and grid[y - 1][x] != 0:
neighbors.append(grid[y - 1][x])
if y + 1 < len(grid) and grid[y + 1][x] != 0:
neighbors.append(grid[y + 1][x])
return neighbors
def build_tile_grid(original_matches: dict) -> List[List[int]]:
matches = deepcopy(original_matches)
sorted_tiles = [tile for tile in sorted(matches, key=lambda key: len(matches[key]))]
num_tiles = len(sorted_tiles)
size = int(sqrt(num_tiles))
grid = [[0 for _ in range(size)] for _ in range(size)]
visited = []
for y in range(size):
for x in range(size):
neighbors = get_neighbors(grid, y, x)
this_tile = get_match(neighbors, sorted_tiles, matches, visited)
for neighbor in neighbors:
matches[this_tile].remove(neighbor)
matches[neighbor].remove(this_tile)
visited.append(this_tile)
grid[y][x] = this_tile
return grid
def fix_orientation(orientation: List[int]):
# cheap fix - may not be general but good enough for this
replace = {0: 2, 1: 3, 2: 0, 3: 1}
if orientation[0] == -1:
orientation[0] = replace[orientation[2]]
if orientation[1] == -1:
orientation[1] = replace[orientation[3]]
def get_flip(tile_id: int, tile_grid: List[List[int]], edges: dict, row: int, col: int, size: int) -> str:
tile_edges = edges[tile_id]
orientation = [-1, -1, -1, -1]
if row - 1 >= 0:
top = tile_grid[row - 1][col]
top_edges = edges[top]
for i, edge in enumerate(tile_edges):
if edge in top_edges:
orientation[0] = i // 2
break
if col + 1 < size:
right = tile_grid[row][col + 1]
right_edges = edges[right]
for i, edge in enumerate(tile_edges):
if edge in right_edges:
orientation[1] = i // 2
break
if row + 1 < size:
bottom = tile_grid[row + 1][col]
bottom_edges = edges[bottom]
for i, edge in enumerate(tile_edges):
if edge in bottom_edges:
orientation[2] = i // 2
break
if col - 1 >= 0:
left = tile_grid[row][col - 1]
left_edges = edges[left]
for i, edge in enumerate(tile_edges):
if edge in left_edges:
orientation[3] = i // 2
break
# print('orientation:', row, col, orientation)
fix_orientation(orientation)
# print('fixed orientation:', orientation)
return ''.join(str(i) for i in orientation[0:2])
def build_image(tile_grid: List[List[int]], edges: dict, tiles: dict) -> List[str]:
size = len(tile_grid)
image = []
for row in range(size):
row_grid = []
for col in range(size):
tile_id = tile_grid[row][col]
flip = get_flip(tile_id, tile_grid, edges, row, col, size)
grid = tiles[tile_id][1:9, 1:9]
if flip == '12':
grid = np.rot90(grid)
elif flip == '23':
grid = np.rot90(np.rot90(grid))
elif flip == '30':
grid = np.rot90(np.rot90(np.rot90(grid)))
elif flip == '03':
grid = np.fliplr(grid)
elif flip == '10':
grid = np.rot90(np.flipud(grid))
elif flip == '21':
grid = np.rot90(np.rot90(np.fliplr(grid)))
elif flip == '32':
grid = np.rot90(np.fliplr(grid))
if col == 0:
row_grid = grid
else:
row_grid = np.concatenate((row_grid, grid), axis=1)
if row == 0:
image = row_grid
else:
image = np.concatenate((image, row_grid), axis=0)
return image
def find_sea_monsters(image: np.array) -> (int, bool):
size = len(image)
found = False
hash_amount = 0
for y in range(size - 2):
for x in range(size - 19):
if image[y][x + 18] == 1 and \
image[y + 1][x] == 1 and \
image[y + 1][x + 5] == 1 and \
image[y + 1][x + 6] == 1 and \
image[y + 1][x + 11] == 1 and \
image[y + 1][x + 12] == 1 and \
image[y + 1][x + 17] == 1 and \
image[y + 1][x + 18] == 1 and \
image[y + 1][x + 19] == 1 and \
image[y + 2][x + 1] == 1 and \
image[y + 2][x + 4] == 1 and \
image[y + 2][x + 7] == 1 and \
image[y + 2][x + 10] == 1 and \
image[y + 2][x + 13] == 1 and \
image[y + 2][x + 16] == 1:
found = True
image[y][x + 18] = 2
image[y + 1][x] = 2
image[y + 1][x + 5] = 2
image[y + 1][x + 6] = 2
image[y + 1][x + 11] = 2
image[y + 1][x + 12] = 2
image[y + 1][x + 17] = 2
image[y + 1][x + 18] = 2
image[y + 1][x + 19] = 2
image[y + 2][x + 1] = 2
image[y + 2][x + 4] = 2
image[y + 2][x + 7] = 2
image[y + 2][x + 10] = 2
image[y + 2][x + 13] = 2
image[y + 2][x + 16] = 2
if found:
hash_amount = np.sum(image == 1)
return hash_amount, found
def part2(matches: dict, edges: dict, tiles: dict) -> int:
tile_grid = build_tile_grid(matches)
image = build_image(tile_grid, edges, tiles)
hashes_left, found = find_sea_monsters(image)
if found:
return hashes_left
image = np.rot90(image)
hashes_left, found = find_sea_monsters(image)
if found:
return hashes_left
image = np.rot90(image)
hashes_left, found = find_sea_monsters(image)
if found:
return hashes_left
image = np.rot90(image)
hashes_left, found = find_sea_monsters(image)
if found:
return hashes_left
image = np.fliplr(np.rot90(image))
hashes_left, found = find_sea_monsters(image)
if found:
return hashes_left
image = np.rot90(image)
hashes_left, found = find_sea_monsters(image)
if found:
return hashes_left
image = np.rot90(image)
hashes_left, found = find_sea_monsters(image)
if found:
return hashes_left
image = np.rot90(image)
hashes_left, found = find_sea_monsters(image)
if found:
return hashes_left
return hashes_left
def part1(matches: dict) -> (int, dict):
product = 1
for tile in matches:
if len(matches[tile]) == 2:
product *= tile
return product
def main():
tiles = parse_input('input/day20.txt')
edges = extract_edges(tiles)
matches = find_matches(edges)
print(f'Part 1: {part1(matches)}')
print(f'Part 2: {part2(matches, edges, tiles)}')
if __name__ == "__main__":
main()