mirror of
https://github.com/peter-tanner/advent-of-code-2020.git
synced 2024-11-30 10:50:17 +08:00
day 17 finally completed, and day 21 done!. Day 20 - only part 1, rest
is crud, does not work
This commit is contained in:
parent
26f3144fd9
commit
a79ed29425
72
17/a.py
72
17/a.py
|
@ -4,5 +4,75 @@ from copy import deepcopy
|
|||
|
||||
ic = [ list(x) for x in open('input').read().splitlines() ]
|
||||
|
||||
[print(x) for x in ic]
|
||||
def generate_plane(space):
|
||||
plane = space[0]
|
||||
empty = [ [ '.' for i in range(len(plane[0])) ] for j in range(len(plane)) ]
|
||||
space.insert(0, empty)
|
||||
space.append(empty)
|
||||
return space
|
||||
space = generate_plane([ic])
|
||||
|
||||
def expand_space(space):
|
||||
space = generate_plane(space)
|
||||
space_ = deepcopy(space)
|
||||
empty = [ '.' for i in range(len(space[0][0])+4) ]
|
||||
for k in range(len(space)-1):
|
||||
for j in range(len(space[k])):
|
||||
space_[k][j] = ['.','.'] + space[k][j] + ['.','.']
|
||||
space_[k].insert(0,deepcopy(empty))
|
||||
space_[k].insert(0,deepcopy(empty))
|
||||
space_[k].append(deepcopy(empty))
|
||||
space_[k].append(deepcopy(empty))
|
||||
# print(empty)
|
||||
# print(len(space_[k]))
|
||||
return space_
|
||||
|
||||
def search(i,j,k,space):
|
||||
# print('search',i,j,k)
|
||||
c = 0
|
||||
for k_ in range(k-1,k+2):
|
||||
for j_ in range(j-1,j+2):
|
||||
for i_ in range(i-1,i+2):
|
||||
if (k_ >= 0 and k_ < len(space) and j_ >= 0 and j_ < len(space[0]) and i_ >= 0 and i_ < len(space[0][0])) and (k_ != k or j_ != j or i_ != i):
|
||||
if space[k_][j_][i_] == '#':
|
||||
c+=1
|
||||
# c+=1
|
||||
# print()
|
||||
# print(c)
|
||||
return c
|
||||
|
||||
def spaceviewer(space): # Take a look at the current state of the system
|
||||
c = 0
|
||||
for z in space:
|
||||
print(c)
|
||||
c+=1
|
||||
[print(y) for y in z]
|
||||
|
||||
# spaceviewer(space)
|
||||
spaceviewer(space)
|
||||
|
||||
# print(len(space_),len(space_[0]),len(space_[0][0]))
|
||||
def update_state(c, space):
|
||||
space = expand_space(space)
|
||||
space_ = deepcopy(space)
|
||||
change = False
|
||||
for k in range(0,len(space_)):
|
||||
for j in range(0,len(space_[0])):
|
||||
for i in range(0,len(space_[0][0])):
|
||||
adj = search(i,j,k,space)
|
||||
if space[k][j][i] == '#' and adj not in [2,3]:
|
||||
space_[k][j][i] = '.'
|
||||
change = True
|
||||
elif space[k][j][i] == '.' and adj == 3:
|
||||
space_[k][j][i] = '#'
|
||||
change = True
|
||||
if change == True and c < 5:
|
||||
c+=1
|
||||
spaceviewer(space_)
|
||||
space_ = update_state(c, space_)
|
||||
return space_
|
||||
|
||||
space_ = update_state(0,space)
|
||||
|
||||
|
||||
print(sum([ sum([ y.count('#') for y in z ]) for z in space_ ]))
|
107
17/b.py
Normal file
107
17/b.py
Normal file
|
@ -0,0 +1,107 @@
|
|||
|
||||
import itertools
|
||||
from copy import deepcopy
|
||||
|
||||
ic = [ list(x) for x in open('17/input').read().splitlines() ]
|
||||
|
||||
def spaceviewer(hyperspace): # Take a look at the current state of the system
|
||||
w_ = 0
|
||||
for w in hyperspace:
|
||||
# print(w)
|
||||
z_ = 0
|
||||
print('w='+str(w_))
|
||||
for z in w:
|
||||
# print(z)
|
||||
print('w='+str(w_)+', z='+str(z_))
|
||||
z_+=1
|
||||
[print(y) for y in z]
|
||||
w_+=1
|
||||
|
||||
def generate_plane(space):
|
||||
plane = space[0]
|
||||
empty = [ [ '.' for i in range(len(plane[0])) ] for j in range(len(plane)) ]
|
||||
space.insert(0, empty)
|
||||
space.append(deepcopy(empty))
|
||||
return space
|
||||
space = generate_plane([ic])
|
||||
|
||||
def expand_space(space):
|
||||
space = generate_plane(generate_plane(space))
|
||||
space_ = deepcopy(space)
|
||||
empty = [ '.' for i in range(len(space[0][0])+4) ]
|
||||
for k in range(len(space)):
|
||||
for j in range(len(space[k])):
|
||||
space_[k][j] = ['.','.'] + space[k][j] + ['.','.']
|
||||
space_[k].insert(0,deepcopy(empty))
|
||||
space_[k].insert(0,deepcopy(empty))
|
||||
space_[k].append(deepcopy(empty))
|
||||
space_[k].append(deepcopy(empty))
|
||||
# print(empty)
|
||||
# print(len(space_[k]))
|
||||
return space_
|
||||
|
||||
def generate_space(z_,y_,x_):
|
||||
return [[['.' for x in range(x_)] for y in range(y_)] for z in range(z_)]
|
||||
|
||||
def expand_hyperspace(hyperspace):
|
||||
# hyperspace
|
||||
for w in range(len(hyperspace)):
|
||||
print()
|
||||
print(len(hyperspace[w]), len(hyperspace[w][0]), len(hyperspace[w][0][0]))
|
||||
hyperspace[w] = expand_space(hyperspace[w])
|
||||
print(len(hyperspace[w]), len(hyperspace[w][0]), len(hyperspace[w][0][0]))
|
||||
empty_space = generate_space(len(hyperspace[0]),len(hyperspace[0][0]),len(hyperspace[0][0][0]))
|
||||
hyperspace.insert(0,deepcopy(empty_space))
|
||||
hyperspace.insert(0,deepcopy(empty_space))
|
||||
hyperspace.append(deepcopy(empty_space))
|
||||
hyperspace.append(deepcopy(empty_space))
|
||||
return hyperspace
|
||||
|
||||
empty_space = generate_space(len(space),len(space[0]),len(space[0][0]))
|
||||
hyperspace = [deepcopy(empty_space), deepcopy(empty_space), space, deepcopy(empty_space), deepcopy(empty_space)]
|
||||
print(hyperspace)
|
||||
spaceviewer(hyperspace)
|
||||
# print(expand_space(hyperspace))
|
||||
# spaceviewer(expand_hyperspace(hyperspace))
|
||||
|
||||
def search(i,j,k,w,space):
|
||||
c = 0
|
||||
# print('search',w,k,j,i)
|
||||
for w_ in range(w-1,w+2):
|
||||
for k_ in range(k-1,k+2):
|
||||
for j_ in range(j-1,j+2):
|
||||
for i_ in range(i-1,i+2):
|
||||
if (w_ >= 0 and w_ < len(space) and k_ >= 0 and k_ < len(space[w_]) and j_ >= 0 and j_ < len(space[w_][k_]) and i_ >= 0 and i_ < len(space[w_][k_][j_])) and (w_ != w or k_ != k or j_ != j or i_ != i):
|
||||
# print(w_,k_,j_,i_," ",len(space),len(space[w_]),len(space[w_][k_]),len(space[w_][k_][j_]))
|
||||
# print(space[w_][k_][j_][i_])
|
||||
if space[w_][k_][j_][i_] == '#':
|
||||
c+=1
|
||||
return c
|
||||
|
||||
# print(len(space_),len(space_[0]),len(space_[0][0]))
|
||||
def update_state(c, space):
|
||||
space = deepcopy(expand_hyperspace(space))
|
||||
# spaceviewer(space)
|
||||
space_ = deepcopy(space)
|
||||
change = False
|
||||
for w in range(0,len(space_)):
|
||||
for k in range(0,len(space_[0])):
|
||||
for j in range(0,len(space_[0][0])):
|
||||
for i in range(0,len(space_[0][0][0])):
|
||||
adj = search(i,j,k,w,space)
|
||||
if space[w][k][j][i] == '#' and adj not in [2,3]:
|
||||
space_[w][k][j][i] = '.'
|
||||
change = True
|
||||
elif space[w][k][j][i] == '.' and adj == 3:
|
||||
space_[w][k][j][i] = '#'
|
||||
change = True
|
||||
if change == True and c < 5:
|
||||
c+=1
|
||||
# spaceviewer(space_)
|
||||
space_ = update_state(c, space_)
|
||||
return space_
|
||||
|
||||
hyperspace_ = update_state(0,hyperspace)
|
||||
|
||||
spaceviewer(hyperspace_)
|
||||
print(sum([sum([ sum([ y.count('#') for y in z ]) for z in w]) for w in hyperspace_ ]))
|
138
20/a.py
Normal file
138
20/a.py
Normal file
|
@ -0,0 +1,138 @@
|
|||
import math
|
||||
data = open('input').read().splitlines()
|
||||
|
||||
id = 0
|
||||
tiles = {}
|
||||
for line in data:
|
||||
if line[0:4] == 'Tile':
|
||||
id = line[5:9]
|
||||
tiles.update({id:[]})
|
||||
elif len(line) > 0:
|
||||
tiles[id].append(line)
|
||||
|
||||
def get_edges(tile):
|
||||
return {
|
||||
tile[0],
|
||||
''.join([tile[j][-1] for j in range(len(tile))]),
|
||||
tile[-1],
|
||||
''.join([tile[j][0] for j in range(len(tile))]),
|
||||
|
||||
tile[0][::-1],
|
||||
''.join([tile[j][-1] for j in range(len(tile))])[::-1],
|
||||
tile[-1][::-1],
|
||||
''.join([tile[j][0] for j in range(len(tile))])[::-1]
|
||||
}
|
||||
|
||||
def get_edges_(tile,phase):
|
||||
if phase == 0:
|
||||
return [
|
||||
tile[0],
|
||||
''.join([tile[j][-1] for j in range(len(tile))]),
|
||||
tile[-1],
|
||||
''.join([tile[j][0] for j in range(len(tile))])
|
||||
]
|
||||
elif phase == 90:
|
||||
return [
|
||||
''.join([tile[j][0] for j in range(len(tile))]),
|
||||
tile[0],
|
||||
''.join([tile[j][-1] for j in range(len(tile))]),
|
||||
tile[-1],
|
||||
]
|
||||
elif phase == 180:
|
||||
return [
|
||||
tile[0][::-1],
|
||||
''.join([tile[j][-1] for j in range(len(tile))])[::-1],
|
||||
tile[-1][::-1],
|
||||
''.join([tile[j][0] for j in range(len(tile))])[::-1]
|
||||
]
|
||||
else:
|
||||
return [
|
||||
''.join([tile[j][-1] for j in range(len(tile))])[::-1],
|
||||
tile[-1][::-1],
|
||||
''.join([tile[j][0] for j in range(len(tile))])[::-1],
|
||||
tile[0][::-1],
|
||||
]
|
||||
|
||||
relationships = {} # <3
|
||||
edges = { id:get_edges(tiles[id]) for id in tiles }
|
||||
for id in edges:
|
||||
tile_edges = edges[id]
|
||||
relationships.update({id:[]})
|
||||
for id_ in edges:
|
||||
if id != id_:
|
||||
adj = list(tile_edges.intersection(edges[id_]))
|
||||
if len(adj) > 0:
|
||||
relationships[id].append(id_)
|
||||
print(adj[0], '=>', id, '->', id_)
|
||||
|
||||
print(math.prod([int(id) for id in relationships if (len(relationships[id]) == 2)]))
|
||||
|
||||
#Part 2!
|
||||
relationships = {} # <3
|
||||
edges = { id:get_edges(tiles[id]) for id in tiles }
|
||||
for id in edges:
|
||||
tile_edges = edges[id]
|
||||
relationships.update({id:[]})
|
||||
for id_ in edges:
|
||||
if id != id_:
|
||||
adj = list(tile_edges.intersection(edges[id_]))
|
||||
if len(adj) > 0:
|
||||
relationships[id].append(id_)
|
||||
print(adj[0], '=>', id, '->', id_)
|
||||
start = [id for id in relationships if (len(relationships[id]) == 2)][0]
|
||||
dim = 3
|
||||
grid = {j:{i:None for i in range(dim)} for j in range(dim)}
|
||||
phase_grid = {j:{i:None for i in range(dim)} for j in range(dim)}
|
||||
d = 0
|
||||
|
||||
def get_adj(x, d, idom, jdom, grid):
|
||||
adj = [None, None]
|
||||
if d-x-1 in jdom:
|
||||
adj[0] = grid[d-x-1][x]
|
||||
if x-1 in idom:
|
||||
adj[1] = grid[d-x][x-1]
|
||||
return adj
|
||||
|
||||
done = set()
|
||||
|
||||
grid[0+d][d] = start
|
||||
phase_grid[0][0] = 0
|
||||
done.add(start)
|
||||
idom = grid[0].keys()
|
||||
jdom = grid.keys()
|
||||
for d in range(1,50):
|
||||
i = 0
|
||||
for x in range(0,d+1):
|
||||
if d-x in jdom and x in idom:
|
||||
# print(d-x, x, get_adj(x, d, idom, jdom, grid))
|
||||
adjacent = get_adj(x, d, idom, jdom, grid)
|
||||
adjacent_ = [ x for x in adjacent if (x != None) ]
|
||||
phase = 0
|
||||
if len(adjacent_) == 1:
|
||||
adj = {x:y for x in relationships[adjacent_[0]] if (y := relationships[x])}
|
||||
# print(adj)
|
||||
# print(set([ x for x in adj if (len(adj[x]) < 4)]).difference(done))
|
||||
add = set([ x for x in adj if (len(adj[x]) < 4)]).difference(done)
|
||||
add = list(add)[0]
|
||||
# add = relationships[adjacent_[0]][i]
|
||||
if adjacent[0] != None: # => Top
|
||||
print( "AAAAAAAAAAAAAAAAAAAAAAAAAAA", adjacent[0])
|
||||
print(tiles[adjacent[0]])
|
||||
else: #adjacent[1] => Left
|
||||
print( "BBBBBBBBBBBBBBBBBBBBBBBB", adjacent[1])
|
||||
i+=1
|
||||
if len(adjacent_) == 2:
|
||||
add = set(relationships[adjacent_[0]]).intersection(set(relationships[adjacent_[1]])).difference(done)
|
||||
# print(add)
|
||||
add = list(add)[0]
|
||||
|
||||
grid[d-x][x] = add
|
||||
done.add(add)
|
||||
# [print(grid[x]) for x in grid]
|
||||
# exit(-1)
|
||||
# i+=1
|
||||
# grid[d][d+1] = rel[1]
|
||||
# print(relationships[start])
|
||||
[print(grid[x]) for x in grid]
|
||||
print()
|
||||
[print(phase_grid[x]) for x in phase_grid]
|
92
20/b.py
Normal file
92
20/b.py
Normal file
|
@ -0,0 +1,92 @@
|
|||
import math
|
||||
data = open('input').read().splitlines()
|
||||
|
||||
id = 0
|
||||
tiles = {}
|
||||
for line in data:
|
||||
if line[0:4] == 'Tile':
|
||||
id = line[5:9]
|
||||
tiles.update({id:[]})
|
||||
elif len(line) > 0:
|
||||
tiles[id].append(line)
|
||||
|
||||
def get_edges(tile):
|
||||
return {
|
||||
tile[0],
|
||||
''.join([tile[j][-1] for j in range(len(tile))]),
|
||||
tile[-1],
|
||||
''.join([tile[j][0] for j in range(len(tile))]),
|
||||
|
||||
tile[0][::-1],
|
||||
''.join([tile[j][-1] for j in range(len(tile))])[::-1],
|
||||
tile[-1][::-1],
|
||||
''.join([tile[j][0] for j in range(len(tile))])[::-1]
|
||||
}
|
||||
|
||||
relationships = {} # <3
|
||||
edges = { id:get_edges(tiles[id]) for id in tiles }
|
||||
for id in edges:
|
||||
tile_edges = edges[id]
|
||||
relationships.update({id:[]})
|
||||
for id_ in edges:
|
||||
if id != id_:
|
||||
# print(tile_edges)
|
||||
adj = list(tile_edges.intersection(edges[id_]))
|
||||
if len(adj) > 0:
|
||||
relationships[id].append(id_)
|
||||
# print(adj[0], '=>', id, '->', id_)
|
||||
|
||||
start = [id for id in relationships if (len(relationships[id]) == 2)][0]
|
||||
dim = 3
|
||||
grid = {j:{i:None for i in range(dim)} for j in range(dim)}
|
||||
phase_grid = {j:{i:None for i in range(dim)} for j in range(dim)}
|
||||
d = 0
|
||||
|
||||
def get_adj(x, d, idom, jdom, grid):
|
||||
adj = [None, None]
|
||||
if d-x-1 in jdom:
|
||||
adj[0] = grid[d-x-1][x]
|
||||
if x-1 in idom:
|
||||
adj[1] = grid[d-x][x-1]
|
||||
return adj
|
||||
|
||||
for i in tiles:
|
||||
tiles[i] = [ list(x) for x in tiles[i] ]
|
||||
|
||||
done = set()
|
||||
|
||||
grid[0+d][d] = start
|
||||
phase_grid[0][0] = 0
|
||||
done.add(start)
|
||||
idom = grid[0].keys()
|
||||
jdom = grid.keys()
|
||||
for d in range(1,50):
|
||||
i = 0
|
||||
for x in range(0,d+1):
|
||||
if d-x in jdom and x in idom:
|
||||
adjacent = get_adj(x, d, idom, jdom, grid)
|
||||
adjacent_ = [ x for x in adjacent if (x != None) ]
|
||||
phase = 0
|
||||
if len(adjacent_) == 1:
|
||||
adj = {x:y for x in relationships[adjacent_[0]] if (y := relationships[x])}
|
||||
add = set([ x for x in adj if (len(adj[x]) < 4)]).difference(done)
|
||||
add = list(add)[0]
|
||||
i+=1
|
||||
if len(adjacent_) == 2:
|
||||
add = set(relationships[adjacent_[0]]).intersection(set(relationships[adjacent_[1]])).difference(done)
|
||||
add = list(add)[0]
|
||||
|
||||
grid[d-x][x] = add
|
||||
done.add(add)
|
||||
|
||||
[print(grid[x]) for x in grid]
|
||||
print()
|
||||
[print(phase_grid[x]) for x in phase_grid]
|
||||
|
||||
|
||||
for i in range(1,3):
|
||||
print(''.join([ tiles[grid[0][i-1]][j][-1] for j in range(len(tiles[grid[0][i-1]])) ]))
|
||||
this_vertical = ''.join([ tiles[grid[0][i]][j][0] for j in range(len(tiles[grid[0][i]])) ])
|
||||
print(this_vertical)
|
||||
print(this_vertical[::-1])
|
||||
print()
|
87
21/a.py
Normal file
87
21/a.py
Normal file
|
@ -0,0 +1,87 @@
|
|||
from copy import deepcopy
|
||||
import itertools
|
||||
|
||||
foods = [ [ set(y.replace(',','').split(' ')) for y in x.replace(')','').split(' (contains ')] for x in open('input').read().splitlines() ]
|
||||
|
||||
recipes = [ x[0] for x in foods ]
|
||||
allergens = [ x[1] for x in foods ]
|
||||
# print(foods)
|
||||
# print(recipes)
|
||||
def get_relationships(values):
|
||||
relationships = {}
|
||||
for r in values:
|
||||
# print(r)
|
||||
for ig in r:
|
||||
if ig not in relationships:
|
||||
relationships.update({ig:set()})
|
||||
i=0
|
||||
for r_ in values:
|
||||
if ig in r_:
|
||||
relationships[ig].add(i)
|
||||
i+=1
|
||||
return relationships
|
||||
# print(get_relationships(recipes))
|
||||
# print(get_relationships(allergens))
|
||||
|
||||
rel = get_relationships(recipes)
|
||||
arel = get_relationships(allergens)
|
||||
non_allergen = []
|
||||
for i in rel:
|
||||
possible = False
|
||||
for a in arel:
|
||||
# print(rel[i].intersection(arel[a]))
|
||||
if len(rel[i].intersection(arel[a])) == len(arel[a]):
|
||||
possible = True
|
||||
break
|
||||
if possible == False:
|
||||
non_allergen.append(i)
|
||||
# print(possible, i)
|
||||
|
||||
c=0
|
||||
for i in non_allergen:
|
||||
c+=list(itertools.chain(*recipes)).count(i)
|
||||
|
||||
print(c)
|
||||
|
||||
|
||||
rel = get_relationships(recipes)
|
||||
arel = get_relationships(allergens)
|
||||
allergen = {}
|
||||
for i in rel:
|
||||
test = []
|
||||
for a in arel:
|
||||
# print(rel[i].intersection(arel[a]))
|
||||
if len(rel[i].intersection(arel[a])) == len(arel[a]):
|
||||
test.append(a)
|
||||
|
||||
if len(test) > 0 :
|
||||
allergen.update({i:test})
|
||||
# print(test, i)
|
||||
|
||||
# print(allergen)
|
||||
sorted_list = {}
|
||||
def alupdate(allergen):
|
||||
allergen_ = deepcopy(allergen)
|
||||
mod = False
|
||||
for al in allergen:
|
||||
als = allergen[al]
|
||||
if len(als) == 1:
|
||||
sorted_list.update({als[0]:al})
|
||||
for al_ in allergen:
|
||||
if als[0] in allergen[al_]:
|
||||
_ = allergen_[al_]
|
||||
_.remove(als[0])
|
||||
allergen_[al_] = _
|
||||
mod = True
|
||||
# print(allergen_)
|
||||
if mod == True:
|
||||
alupdate(allergen_)
|
||||
else:
|
||||
for al in allergen_:
|
||||
if len(allergen_[al]) == 1:
|
||||
sorted_list.update({allergen_[al][0]:al})
|
||||
|
||||
alupdate(allergen)
|
||||
|
||||
# print(sorted_list)
|
||||
print(','.join([sorted_list[x] for x in sorted(sorted_list)]))
|
Loading…
Reference in New Issue
Block a user