Day 20, feeble part two attempt (Python)

This commit is contained in:
akp 2020-12-20 18:47:06 +00:00
parent 566d0c6fc3
commit 648cd89bfc
No known key found for this signature in database
GPG key ID: D3E7EAA31B39637E
4 changed files with 72 additions and 43 deletions

View file

@ -9,6 +9,11 @@
"expected": 20899048083289
}
],
"two": []
"two": [
{
"input": "Tile 2311:\n..##.#..#.\n##..#.....\n#...##..#.\n####.#...#\n##.##.###.\n##...#.###\n.#.#.#..##\n..#....#..\n###...#.#.\n..###..###\n\nTile 1951:\n#.##...##.\n#.####...#\n.....#..##\n#...######\n.##.#....#\n.###.#####\n###.##.##.\n.###....#.\n..#.#..#.#\n#...##.#..\n\nTile 1171:\n####...##.\n#..##.#..#\n##.#..#.#.\n.###.####.\n..###.####\n.##....##.\n.#...####.\n#.##.####.\n####..#...\n.....##...\n\nTile 1427:\n###.##.#..\n.#..#.##..\n.#.##.#..#\n#.#.#.##.#\n....#...##\n...##..##.\n...#.#####\n.#.####.#.\n..#..###.#\n..##.#..#.\n\nTile 1489:\n##.#.#....\n..##...#..\n.##..##...\n..#...#...\n#####...#.\n#..#.#.#.#\n...#.#.#..\n##.#...##.\n..##.##.##\n###.##.#..\n\nTile 2473:\n#....####.\n#..#.##...\n#.##..#...\n######.#.#\n.#...#.#.#\n.#########\n.###.#..#.\n########.#\n##...##.#.\n..###.#.#.\n\nTile 2971:\n..#.#....#\n#...###...\n#.#.###...\n##.##..#..\n.#####..##\n.#..####.#\n#..#.#..#.\n..####.###\n..#.#.###.\n...#.#.#.#\n\nTile 2729:\n...#.#.#.#\n####.#....\n..#.#.....\n....#..#.#\n.##..##.#.\n.#.####...\n####.#.#..\n##.####...\n##..#.##..\n#.##...##.\n\nTile 3079:\n#.#.#####.\n.#..######\n..#.......\n######....\n####.#..#.\n.#...#.##.\n#.#####.##\n..#.###...\n..#.......\n..#.###...\n",
"expected": 273
}
]
}
}

View file

@ -1,23 +1,61 @@
from typing import List, Set
from typing import List, Set, Dict, Tuple
import re
def get_tile_edges(tile: List[List[str]]) -> List[str]:
# returns edges in order: top, bottom, left, right
edges = []
edges.append("".join(tile[0]))
edges.append("".join(tile[-1]))
edges.append("".join([tile[x][0] for x in range(len(tile))]))
edges.append("".join([tile[x][-1] for x in range(len(tile))]))
return edges
class Tile:
components: List[List[str]]
edges: Set[str]
edges: List[str]
number: int
def __init__(self, instr:str) -> None:
def __init__(self, instr: str) -> None:
split_input = instr.strip().split("\n")
self.number = int(re.search(r"Tile (\d+)", split_input[0]).group(1))
self.components = [list(x) for x in split_input[1:]]
self.edges = []
self.edges.append(split_input[1])
self.edges.append(split_input[-1])
self.edges.append("".join([self.components[x][0] for x in range(len(self.components))]))
self.edges.append("".join([self.components[x][-1] for x in range(len(self.components))]))
self.edges = get_tile_edges(self.components)
def parse(instr: str) -> List[Tile]:
return [Tile(x) for x in instr.strip().split("\n\n")]
def get_edge_information(tiles:List[Tile]) -> Tuple[Dict[str, Set[int]], Dict[int, int]]:
edges = {}
shared_edge_count = {x.number: 0 for x in tiles}
# find dictionary of edges and the tiles that have them
for tile in tiles:
for edge in tile.edges:
if edge not in edges:
edges[edge] = set([tile.number])
else:
edges[edge].add(tile.number)
# combine edges that are reversed versions of each other
to_del = []
for edge in edges:
rev = "".join(reversed(edge))
if rev in edges and edge not in to_del:
edges[edge].update(edges[rev])
to_del.append(rev)
for r in to_del:
del edges[r]
# count the number of shared edges each tile has
for tile in tiles:
for x in edges:
tn = edges[x]
if tile.number in tn and len(tn) > 1:
shared_edge_count[tile.number] += 1
return edges, shared_edge_count

View file

@ -5,41 +5,10 @@ from pprint import pprint
def partOne(instr: str) -> int:
tiles = parse(instr)
edges = {}
# find dictionary of edges and the tiles that have them
for tile in tiles:
for edge in tile.edges:
if edge not in edges:
edges[edge] = set([tile.number])
else:
edges[edge].add(tile.number)
to_del = []
for edge in edges:
rev = "".join(reversed(edge))
if rev in edges and edge not in to_del:
edges[edge].update(edges[rev])
to_del.append(rev)
for r in to_del:
del edges[r]
# count the number of shared edges each tile has
shared_edge_count = {x.number:0 for x in tiles}
for tile in tiles:
for x in edges:
tn = edges[x]
if tile.number in tn and len(tn) > 1:
shared_edge_count[tile.number] += 1
_, shared_edge_count = get_edge_information(tiles)
# find the product of all tile numbers that have 2 matching edges
pprint(shared_edge_count)
c = 1
for x in shared_edge_count:
if shared_edge_count[x] == 2:

View file

@ -2,5 +2,22 @@ from common import *
def partTwo(instr: str) -> int:
input_list = parse(instr)
tiles_list = parse(instr)
edges, shared_edge_count = get_edge_information(tiles_list)
# from here onwards, it's going to make more sense to use a dictionary that has tile numbers to the tile object
tiles = {}
for tile in tiles_list:
tiles[tile.number] = tile
used_tiles = []
matrix = {}
# get a single corner, set that as the start point
for tile_id in shared_edge_count:
if shared_edge_count[tile_id] == 2:
matrix[(0, 0)] = tiles[tile_id].components
print(matrix)
return 0