Skip to content

Instantly share code, notes, and snippets.

@napisani
Created January 5, 2021 04:51
Show Gist options
  • Save napisani/4f7651a3904de02cc54c762354825dcf to your computer and use it in GitHub Desktop.
Save napisani/4f7651a3904de02cc54c762354825dcf to your computer and use it in GitHub Desktop.
AoC2020-Day24pt1
from typing import List
all_tiles = dict()
vector_map = {
'ne': (1, 1),
'nw': (-1, 1),
'e': (2, 0),
'w': (-2, 0),
'sw': (-1, -1),
'se': (1, -1)
}
class Tile:
def __init__(self, point=(0, 0)):
self.point = point
self.white = True
self.flip_cnt = 0
def add(self, coord):
d_x, d_y = coord
x, y = self.point
self.point = (x + d_x, y + d_y)
def flip(self):
self.white = not self.white
self.flip_cnt += 1
def __repr__(self):
return f'{hex(id(self))} white: {self.white} coord: {self.point} flip_cnt: {self.flip_cnt}'
def parse_dir_tokens(line: str) -> List[str]:
tokens = []
idx = 0
while idx < len(line):
ch = line[idx]
if idx <= len(line) - 2:
ch2 = line[idx + 1]
if ch == 's':
if ch2 == 'w':
tokens.append('sw')
idx += 1
elif ch2 == 'e':
tokens.append('se')
idx += 1
elif ch == 'n':
if ch2 == 'w':
tokens.append('nw')
idx += 1
elif ch2 == 'e':
tokens.append('ne')
idx += 1
else:
tokens.append(ch)
else:
tokens.append(ch)
idx += 1
return tokens
def flip_tile(instrs: List[str]):
tile = Tile()
for ist in instrs:
tile.add(vector_map[ist])
if all_tiles.get(tile.point) is None:
all_tiles[tile.point] = tile
print(f'added {tile}')
else:
tile = all_tiles[tile.point]
print(f'reused {tile}')
tile.flip()
print(f'flipped {tile}')
def day24():
with open('day24input.txt', 'r') as f:
for line in f.readlines():
tokens = parse_dir_tokens(line.strip('\n'))
print(tokens)
flip_tile( tokens)
for key in all_tiles:
print(all_tiles[key])
print('\n')
for key in all_tiles:
if all_tiles[key].flip_cnt == 1:
print(all_tiles[key])
print('\n')
for key in all_tiles:
if all_tiles[key].flip_cnt > 1:
print(all_tiles[key])
print(sum([1 for key in all_tiles if not all_tiles[key].white]))
if __name__ == '__main__':
day24()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment