Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Day 24 of Advent of Code 2020 Solution"""
- from collections import defaultdict, Counter
- from itertools import chain
- import re
- class HexGrid:
- directions = {
- "ne": (1, 0, -1),
- "e": (1, -1, 0),
- "se": (0, -1, 1),
- "sw": (-1, 0, 1),
- "w": (-1, 1, 0),
- "nw": (0, 1, -1),
- }
- def __init__(self):
- self.grid = defaultdict(bool)
- def add_tile(self, move):
- x, y, z = 0, 0, 0
- for d in move:
- x += self.directions[d][0]
- y += self.directions[d][1]
- z += self.directions[d][2]
- if (x, y, z) in self.grid:
- self.flip_tile((x, y, z))
- else:
- self.grid[(x, y, z)] = True
- def flip_tile(self, location):
- if self.grid[location]:
- self.grid[location] = False
- else:
- self.grid[location] = True
- @property
- def black(self):
- return sum([*self.grid.values()])
- def __iter__(self):
- return self
- def __next__(self):
- grid = defaultdict(bool)
- all_adj_tiles = []
- for tile in self.grid:
- if self.grid[tile]:
- adj_tiles = [
- tuple([_t + _d for _t, _d in zip(tile, d)])
- for d in self.directions.values()
- ]
- n_adjacent = sum([self.grid[t] for t in adj_tiles if t in self.grid])
- grid[tile] = True if 0 < n_adjacent <= 2 else False
- all_adj_tiles.append(adj_tiles)
- adjacent_count = Counter([*chain.from_iterable(all_adj_tiles)])
- for tile in adjacent_count:
- if not self.grid[tile] and adjacent_count[tile] == 2:
- grid[tile] = True
- self.grid = grid
- def directions_io(file_location):
- pattern = r"(ne)|(e)|(se)|(sw)|(w)|(nw)"
- directions = []
- with open(file_location, "r") as f:
- for line in f.readlines():
- directions.append([l.group() for l in re.finditer(pattern, line)])
- return directions
- def part_a(file_location):
- directions = directions_io(file_location)
- hex = HexGrid()
- for d in directions:
- hex.add_tile(d)
- return hex.black
- def part_b(file_location):
- directions = directions_io(file_location)
- hex = HexGrid()
- for d in directions:
- hex.add_tile(d)
- for _ in range(100):
- next(hex)
- return hex.black
- if __name__ == "__main__":
- file_location = r"data\day24.txt"
- print(part_a(file_location))
- print(part_b(file_location))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement