Day 20, part one (Python)

This commit is contained in:
akp 2020-12-20 17:50:25 +00:00
parent 5ae9201115
commit 566d0c6fc3
No known key found for this signature in database
GPG key ID: D3E7EAA31B39637E
2 changed files with 16 additions and 5 deletions

View file

@ -1,3 +1,5 @@
# [Day 20: Jurassic Jigsaw](https://adventofcode.com/2020/day/20)
Gave up on this one. I was struggling to find a decent logical way to do it manually, yet alone write a set of rules to get a computer to do it.
~~Gave up on this one. I was struggling to find a decent logical way to do it manually, yet alone write a set of rules to get a computer to do it.~~
Okay, so I had a lightbulb moment and came back to take another look at part one, which I got pretty easily. I'm going to take another crack on part two, since tiles have certain properties that I didn't realise were present.

View file

@ -7,6 +7,8 @@ def partOne(instr: str) -> int:
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:
@ -18,13 +20,15 @@ def partOne(instr: str) -> int:
for edge in edges:
rev = "".join(reversed(edge))
if rev in edges:
edges[edge] = edges[edge].union(edges[rev])
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:
@ -32,8 +36,13 @@ def partOne(instr: str) -> int:
if tile.number in tn and len(tn) > 1:
shared_edge_count[tile.number] += 1
# find the product of all tile numbers that have 2 matching edges
pprint(shared_edge_count)
pprint(edges)
c = 1
for x in shared_edge_count:
if shared_edge_count[x] == 2:
c *= x
return 0
return c