Created
December 20, 2020 06:12
-
-
Save SebbyLaw/249d92f1d625768f90ce46f1bf93e0cf to your computer and use it in GitHub Desktop.
AOC 2020 day 20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import operator | |
def is_match(s, o): | |
if s.raw == o.raw: | |
return True | |
if s.raw[::-1] == o.raw: | |
return True | |
return False | |
class Side: | |
def __init__(self, n): | |
self.raw = n | |
self.matches = False | |
self._tile = None | |
self.matching_side = None | |
def __eq__(self, other): | |
if self._tile is other._tile: | |
return False | |
else: | |
x = is_match(self, other) | |
if x: | |
self.matching_side = other | |
return x | |
class Tile: | |
def __init__(self, n): | |
til, string = n.split(':\n') | |
self.id = int(til.split(' ')[-1]) | |
self.raw = string | |
self.sides = s = [] | |
x = string.splitlines() | |
s.append(Side(x[0])) | |
s.append(Side(x[-1])) | |
s.append(Side(''.join(map(lambda t: t[0], x)))) | |
s.append(Side(''.join(map(lambda t: t[-1], x)))) | |
for side in self.sides: | |
side._tile = self | |
@property | |
def num_matches(self): | |
return sum(map(lambda m: m.matches, self.sides)) | |
tiles = [*map(Tile, inp.split('\n\n'))] | |
for tile in tiles: | |
for side in tile.sides: | |
if any(os == side for ot in tiles for os in ot.sides): | |
side.matches = True | |
corners = [tile for tile in tiles if tile.num_matches == 2] | |
print('part 1:', functools.reduce(operator.mul, map(lambda t: t.id, corners))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment