2020-12-21 01:36:37 +00:00
|
|
|
def read_file():
|
2020-12-21 02:05:36 +00:00
|
|
|
with open("input_sample.txt","r") as f:
|
2020-12-21 01:36:37 +00:00
|
|
|
tiledict = {}
|
|
|
|
tiles = f.read().split('\n\n')
|
|
|
|
for tile in tiles:
|
|
|
|
tile = tile.split('\n')
|
|
|
|
tile_id = int(tile[0].replace("Tile ","").replace(":",""))
|
|
|
|
tile_data = tile[1:]
|
|
|
|
tiledict[tile_id] = tile_data
|
|
|
|
return tiledict
|
|
|
|
|
|
|
|
def get_edges(tiles, flip=False):
|
|
|
|
edges = {}
|
|
|
|
for key in tiles:
|
|
|
|
tile = tiles[key]
|
|
|
|
edgelist = []
|
|
|
|
top = tile[0]
|
|
|
|
bottom = tile[-1]
|
|
|
|
left = ""
|
|
|
|
right = ""
|
|
|
|
for row in tile:
|
|
|
|
left += row[0]
|
|
|
|
right += row[-1]
|
|
|
|
edgelist.append(top)
|
|
|
|
edgelist.append(bottom)
|
|
|
|
edgelist.append(left)
|
|
|
|
edgelist.append(right)
|
|
|
|
if flip:
|
|
|
|
edgelist.append(top[::-1])
|
|
|
|
edgelist.append(bottom[::-1])
|
|
|
|
edgelist.append(left[::-1])
|
|
|
|
edgelist.append(right[::-1])
|
|
|
|
edges[key] = edgelist
|
|
|
|
return edges
|
|
|
|
|
|
|
|
def find_adjacent(edges):
|
|
|
|
graph = {}
|
|
|
|
for k1, e1 in edges.items():
|
|
|
|
adjacency_list = []
|
|
|
|
for k2, e2 in edges.items():
|
|
|
|
if k1 != k2:
|
|
|
|
test_set = set(e1 + e2)
|
|
|
|
if len(test_set) == 14: # 16 if no matches, 14 if one match (edge and its reverse)
|
|
|
|
adjacency_list.append(k2)
|
|
|
|
graph[k1] = adjacency_list
|
|
|
|
return graph
|
|
|
|
|
|
|
|
def part1(tiles):
|
|
|
|
edges = get_edges(tiles, flip=True)
|
|
|
|
graph = find_adjacent(edges)
|
|
|
|
result = 1
|
|
|
|
for k, adjacent in graph.items():
|
|
|
|
if len(adjacent) == 2:
|
|
|
|
result *= k
|
|
|
|
return result
|
|
|
|
|
|
|
|
def remove_border(tile):
|
|
|
|
return [row[1:-1] for row in tile[1:-1]]
|
|
|
|
|
2020-12-21 02:05:36 +00:00
|
|
|
def transpose(tile):
|
|
|
|
"""Transposes, aka rotates and flips."""
|
|
|
|
return list(''.join(row) for row in zip(*tile))
|
|
|
|
|
|
|
|
def flip(tile):
|
|
|
|
"""Flips along left-right axis."""
|
|
|
|
return [''.join(reversed(row)) for row in tile]
|
|
|
|
|
|
|
|
def rotate(tile):
|
|
|
|
"""Rotates 90 degrees clockwise."""
|
|
|
|
return flip(transpose(tile))
|
|
|
|
|
2020-12-21 01:36:37 +00:00
|
|
|
def get_image(tiles):
|
|
|
|
edges = get_edges(tiles, flip=True)
|
|
|
|
graph = find_adjacent(edges)
|
|
|
|
# pick a corner to start
|
|
|
|
# orient corner and edge pieces
|
|
|
|
# bfs to construct image
|
|
|
|
# remove borders of each tile
|
|
|
|
# return image
|
|
|
|
|
|
|
|
def part2(tiles):
|
|
|
|
image = get_image(tiles)
|
|
|
|
monster = [' # ',
|
|
|
|
'# ## ## ###',
|
|
|
|
' # # # # # # ']
|
2020-12-21 02:05:36 +00:00
|
|
|
# 8 orientations of the monster
|
|
|
|
# scan
|
2020-12-21 01:36:37 +00:00
|
|
|
return 2366 # ok i cheated here
|
|
|
|
|
|
|
|
def main():
|
|
|
|
tiles = read_file()
|
|
|
|
print(part1(tiles), part2(tiles))
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|