-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_20b.py
314 lines (282 loc) · 10.1 KB
/
day_20b.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
307
308
309
310
311
312
313
314
#!/usr/bin/python3
import math
import copy
import builtins
class Tile:
def __init__(self):
self.used = False
self.flipped = False
self.id = -1
self.rotation = 0
self.neighbour_ids = set()
self.image = list()
self.image_str = list()
self.edges = list()
def createEdges(self):
left = str()
right = str()
for line in self.image_str:
left = left + line[0]
right = right + line[len(line) - 1]
self.edges.append(self.image_str[0])
self.edges.append(right)
self.edges.append(self.image_str[len(self.image_str) - 1])
self.edges.append(left)
def getRightEdge(self):
return self.edges[1]
def getBottomEdge(self):
return self.edges[2]
def Rotate(self):
self.edges.insert(0, self.edges[3])
self.edges.pop()
self.edges[0] = self.edges[0][::-1]
self.edges[2] = self.edges[2][::-1]
self.rotation = self.rotation + 1
self.rotation = self.rotation % 4
def Flip(self):
temp = copy.deepcopy(self.edges[0])
self.edges[0] = copy.deepcopy(self.edges[1])
self.edges[1] = copy.deepcopy(temp)
temp = copy.deepcopy(self.edges[3])
self.edges[3] = copy.deepcopy(self.edges[2])
self.edges[2] = copy.deepcopy(temp)
for i in range(0, len(self.edges)):
self.edges[i] = self.edges[i][::-1]
self.flipped = not self.flipped
def applyRotationsAndFlipToImage(self):
if self.flipped:
new_image = copy.deepcopy(self.image)
for i in range(0, len(self.image)):
for j in range(0, len(self.image[i])):
new_image[len(self.image[i]) - j - 1][
len(self.image) - i - 1
] = self.image[i][j]
self.image = copy.deepcopy(new_image)
for r in range(0, self.rotation):
new_image = copy.deepcopy(self.image)
for i in range(0, len(self.image)):
for j in range(0, len(self.image[i])):
new_image[j][len(self.image) - i - 1] = self.image[i][j]
self.image = copy.deepcopy(new_image)
def RotateImage(image):
new_image = copy.deepcopy(image)
for i in range(0, len(image)):
for j in range(0, len(image[i])):
new_image[j][len(image) - i - 1] = image[i][j]
return new_image
def FlipImage(image):
new_image = copy.deepcopy(image)
for i in range(0, len(image)):
for j in range(0, len(image[i])):
new_image[len(image[i]) - j - 1][len(image) - i - 1] = image[i][j]
return new_image
def OrientTileToMatchOnSide(edge, cur_tile, edge_dir):
for i in range(0, 4):
if edge == cur_tile.edges[edge_dir]:
return
else:
cur_tile.Rotate()
cur_tile.Flip()
for i in range(0, 4):
if edge == cur_tile.edges[edge_dir]:
return
else:
cur_tile.Rotate()
def CheckForPattern(complete_image, pattern):
max_row_size = 0
relevant_points = list()
for i in range(0, len(pattern)):
relevant_points.append([])
for j in range(0, len(pattern[i])):
max_row_size = max(max_row_size, len(pattern[i]))
if pattern[i][j] == "#":
relevant_points[i].append(j)
count = 0
for i in range(0, len(complete_image) - len(relevant_points)):
for j in range(0, len(complete_image[i]) - max_row_size):
match = True
for pi in range(0, len(relevant_points)):
for pj in range(0, len(relevant_points[pi])):
if complete_image[i + pi][j + relevant_points[pi][pj]] != "#":
match = False
break
if not match:
break
if match:
count = count + 1
return count
def backtrack(puzzle_size, count, grid, edge_map, tiles):
if count == len(tiles):
return True
row = math.floor(count / puzzle_size)
col = int(count % puzzle_size)
if col == 0:
bottom_edge = tiles[grid[row - 1][col]].getBottomEdge()
ids = edge_map[bottom_edge]
for id in ids:
if id == grid[row - 1][0] or tiles[id].used:
continue
cur_tile = tiles[id]
cur_tile.used = True
grid[row][col] = cur_tile.id
OrientTileToMatchOnSide(bottom_edge, cur_tile, 0)
if backtrack(puzzle_size, count + 1, grid, edge_map, tiles):
return True
else:
cur_tile.used = False
grid[row][col] = -1
elif row == 0:
right_edge = tiles[grid[row][col - 1]].getRightEdge()
ids = edge_map[right_edge]
for id in ids:
if id == grid[row][col - 1] or tiles[id].used:
continue
cur_tile = tiles[id]
cur_tile.used = True
grid[row][col] = cur_tile.id
OrientTileToMatchOnSide(right_edge, cur_tile, 3)
if backtrack(puzzle_size, count + 1, grid, edge_map, tiles):
return True
else:
cur_tile.used = False
grid[row][col] = -1
elif row != 0:
bottom_edge = tiles[grid[row - 1][col]].getBottomEdge()
ids = edge_map[bottom_edge]
for id in ids:
if id == grid[row - 1][col] or tiles[id].used:
continue
cur_tile = tiles[id]
cur_tile.used = True
grid[row][col] = cur_tile.id
OrientTileToMatchOnSide(bottom_edge, cur_tile, 0)
right_edge = tiles[grid[row][col - 1]].getRightEdge()
if cur_tile.edges[3] == right_edge and backtrack(
puzzle_size, count + 1, grid, edge_map, tiles
):
return True
else:
cur_tile.used = False
grid[row][col] = -1
return False
def main():
f = open("../input/day_20_input")
new_tile = Tile()
tiles = dict()
edge_map = dict()
for line in f:
line = line.strip("\n")
if line == "":
new_tile.createEdges()
tiles[new_tile.id] = copy.deepcopy(new_tile)
new_tile = Tile()
continue
elif line[0:4] == "Tile":
new_tile.id = int(line[5 : len(line) - 1])
else:
new_tile.image_str.append(line)
new_tile.image.append([])
for char in line:
new_tile.image[len(new_tile.image) - 1].append(char)
if new_tile.id != -1:
new_tile.createEdges()
tiles[new_tile.id] = new_tile
for tile_id in tiles:
tile = tiles[tile_id]
for edge in tile.edges:
if edge not in edge_map:
edge_map[edge] = [tile_id]
r_edge = edge[::-1]
edge_map[r_edge] = [tile_id]
else:
if tile_id not in edge_map[edge]:
edge_map[edge].append(tile_id)
r_edge = edge[::-1]
edge_map[r_edge].append(tile_id)
for edge in edge_map:
ids = edge_map[edge]
for i in range(0, len(ids)):
for j in range(i + 1, len(ids)):
if j not in tiles[ids[i]].neighbour_ids:
tiles[ids[i]].neighbour_ids.add(ids[j])
if i not in tiles[ids[j]].neighbour_ids:
tiles[ids[j]].neighbour_ids.add(ids[i])
corner_ids = list()
for id in tiles:
tile = tiles[id]
if len(tile.neighbour_ids) == 2:
corner_ids.append(id)
found = False
count = 1
puzzle_size = int(math.sqrt(len(tiles)))
grid = list()
for i in range(0, puzzle_size):
grid.append([-1] * puzzle_size)
for corner_id in corner_ids:
grid[0][0] = corner_id
tiles[corner_id].used = True
for i in range(0, 4):
if backtrack(puzzle_size, count, grid, edge_map, tiles):
found = True
break
tiles[corner_id].Rotate()
if found:
break
tiles[corner_id].Flip()
for i in range(0, 4):
if backtrack(puzzle_size, count, grid, edge_map, tiles):
found = True
break
tiles[corner_id].Rotate()
if found:
break
else:
tiles[corner_id].used = False
complete_image = list()
for i in range(0, puzzle_size):
for j in range(0, puzzle_size):
tiles[grid[i][j]].applyRotationsAndFlipToImage()
tile_img = tiles[grid[i][j]].image
for k in range(0, len(tile_img) - 2):
# if (j == 0):
# complete_image.append([])
if i * (len(tile_img) - 2) + k >= len(complete_image):
complete_image.append([])
complete_image[i * (len(tile_img) - 2) + k].extend(
tile_img[k + 1][1 : len(tile_img[k]) - 1]
)
pattern_temp = [
" # ",
"# ## ## ###",
" # # # # # # ",
]
pattern = list()
for i in range(0, len(pattern_temp)):
pattern.append([])
for char in pattern_temp[i]:
pattern[i].append(char)
monster_size = 0
for row in pattern:
for ele in row:
if ele == "#":
monster_size = monster_size + 1
n_monsters = 0
for i in range(0, 2):
for j in range(0, 4):
n_monsters = CheckForPattern(complete_image, pattern)
if n_monsters > 0:
break
complete_image = RotateImage(complete_image)
if n_monsters > 0:
break
complete_image = FlipImage(complete_image)
roughness = 0
for i in range(0, len(complete_image)):
for j in range(0, len(complete_image[i])):
if complete_image[i][j] == "#":
roughness = roughness + 1
roughness = roughness - (n_monsters * monster_size)
print(roughness)
return roughness
if __name__ == "__main__":
main()