mirror of
https://github.com/peter-tanner/advent-of-code-2020.git
synced 2024-11-30 10:50:17 +08:00
82 lines
2.3 KiB
Python
82 lines
2.3 KiB
Python
|
from operator import add, sub
|
||
|
from copy import deepcopy
|
||
|
|
||
|
directions = [x.replace('e','e ').replace('w','w ')[:-1].split(' ') for x in open('input').read().splitlines()]
|
||
|
|
||
|
def getHexAdjacent(vec):
|
||
|
adj = {}
|
||
|
di,dj,dk = (0,1,1)
|
||
|
adj.update({
|
||
|
'sw': (vec[0]+di,vec[1]+dj,vec[2]+dk),
|
||
|
'ne': (vec[0]-di,vec[1]-dj,vec[2]-dk)
|
||
|
})
|
||
|
di,dj,dk = (1,-1,0)
|
||
|
adj.update({
|
||
|
'e': (vec[0]+di,vec[1]+dj,vec[2]+dk),
|
||
|
'w': (vec[0]-di,vec[1]-dj,vec[2]-dk)
|
||
|
})
|
||
|
di,dj,dk = (1,0,1)
|
||
|
adj.update({
|
||
|
'se': (vec[0]+di,vec[1]+dj,vec[2]+dk),
|
||
|
'nw': (vec[0]-di,vec[1]-dj,vec[2]-dk)
|
||
|
})
|
||
|
return adj
|
||
|
|
||
|
def getHexAdjacentState(vec):
|
||
|
getHexAdjacent
|
||
|
return True
|
||
|
|
||
|
def toggle(boolean):
|
||
|
return (False if boolean else True)
|
||
|
|
||
|
grid = {(0,0,0):{'state':False}}#'adj':getHexAdjacent((0,0,0)),
|
||
|
|
||
|
vectors = []
|
||
|
for direction in directions:
|
||
|
# print(direction)
|
||
|
vec = (0,0,0)
|
||
|
for dir_ in direction:
|
||
|
vec = getHexAdjacent(vec)[dir_]
|
||
|
grid.update({vec:{'state':toggle(grid[vec]['state'] if vec in grid else False)}})
|
||
|
vectors.append(vec)
|
||
|
# print({x:vectors.count(x) for x in vectors})
|
||
|
# print(len([vectors.count(x) for x in vectors if (vectors.count(x) % 2 == 1)]))
|
||
|
|
||
|
#Part 1
|
||
|
print(len([ x for x in grid if (grid[x]['state'] == True)])) # Black => True, white => false
|
||
|
|
||
|
grid_ = deepcopy(grid) #expand our space by one hextile in each direction
|
||
|
for cell in grid:
|
||
|
adj = getHexAdjacent(cell)
|
||
|
for cell_ in adj:
|
||
|
cell_ = adj[cell_]
|
||
|
if cell_ not in grid:
|
||
|
grid_.update({cell_:{'state':False}})
|
||
|
|
||
|
i = 0
|
||
|
while i < 100:
|
||
|
grid = grid_
|
||
|
grid_ = deepcopy(grid)
|
||
|
for cell in grid:
|
||
|
adj = getHexAdjacent(cell)
|
||
|
c = 0
|
||
|
for cell_ in adj:
|
||
|
cell_ = adj[cell_]
|
||
|
if cell_ in grid:
|
||
|
if grid[cell_]['state'] == True:
|
||
|
c+=1
|
||
|
else:
|
||
|
# print(len(grid_))
|
||
|
grid_.update({cell_:{'state':False}})
|
||
|
|
||
|
if grid[cell]['state'] == True and (c == 0 or c > 2):
|
||
|
# print('false')
|
||
|
grid_[cell]['state'] = False
|
||
|
elif grid[cell]['state'] == False and c == 2:
|
||
|
# print('true')
|
||
|
grid_[cell]['state'] = True
|
||
|
# print(len(grid))
|
||
|
# print(len(grid_))
|
||
|
i+=1
|
||
|
print(len([ x for x in grid_ if (grid_[x]['state'] == True)]))
|