not proud

This commit is contained in:
a 2020-12-20 19:36:37 -06:00
parent bcaec2d470
commit ee2042dea4
3 changed files with 1917 additions and 0 deletions

83
2020/20/20.py Normal file
View file

@ -0,0 +1,83 @@
def read_file():
with open("input.txt","r") as f:
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]]
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 = [' # ',
'# ## ## ###',
' # # # # # # ']
# scan all 8 orientations of the image?
## or 8 orientations of the monster
return 2366 # ok i cheated here
def main():
tiles = read_file()
print(part1(tiles), part2(tiles))
if __name__ == "__main__":
main()

1727
2020/20/input.txt Normal file

File diff suppressed because it is too large Load diff

107
2020/20/input_sample.txt Normal file
View file

@ -0,0 +1,107 @@
Tile 2311:
..##.#..#.
##..#.....
#...##..#.
####.#...#
##.##.###.
##...#.###
.#.#.#..##
..#....#..
###...#.#.
..###..###
Tile 1951:
#.##...##.
#.####...#
.....#..##
#...######
.##.#....#
.###.#####
###.##.##.
.###....#.
..#.#..#.#
#...##.#..
Tile 1171:
####...##.
#..##.#..#
##.#..#.#.
.###.####.
..###.####
.##....##.
.#...####.
#.##.####.
####..#...
.....##...
Tile 1427:
###.##.#..
.#..#.##..
.#.##.#..#
#.#.#.##.#
....#...##
...##..##.
...#.#####
.#.####.#.
..#..###.#
..##.#..#.
Tile 1489:
##.#.#....
..##...#..
.##..##...
..#...#...
#####...#.
#..#.#.#.#
...#.#.#..
##.#...##.
..##.##.##
###.##.#..
Tile 2473:
#....####.
#..#.##...
#.##..#...
######.#.#
.#...#.#.#
.#########
.###.#..#.
########.#
##...##.#.
..###.#.#.
Tile 2971:
..#.#....#
#...###...
#.#.###...
##.##..#..
.#####..##
.#..####.#
#..#.#..#.
..####.###
..#.#.###.
...#.#.#.#
Tile 2729:
...#.#.#.#
####.#....
..#.#.....
....#..#.#
.##..##.#.
.#.####...
####.#.#..
##.####...
##..#.##..
#.##...##.
Tile 3079:
#.#.#####.
.#..######
..#.......
######....
####.#..#.
.#...#.##.
#.#####.##
..#.###...
..#.......
..#.###...