Skip to content

Instantly share code, notes, and snippets.

@mei-li
Created December 19, 2020 17:45
Show Gist options
  • Save mei-li/f387769eff02526ee49173f9818d86f2 to your computer and use it in GitHub Desktop.
Save mei-li/f387769eff02526ee49173f9818d86f2 to your computer and use it in GitHub Desktop.
def in_level(x, y, z, w, sizex, sizey, sizez, sizew):
return 0 <= x < sizex and 0 <= y < sizey and 0 <= z < sizez and 0 <= w < sizew
def active_neighbours(x, y, z, w, sizex, sizey, sizez, sizew, levels):
active = 0
for u in range(w - 1, w + 2):
for k in range(z - 1, z + 2):
for j in range(y - 1, y + 2):
for i in range(x - 1, x + 2):
if (i, j, k, u) == (x, y, z, w):
continue
if in_level(i, j, k, u, sizex, sizey, sizez, sizew) and levels[u][k][j][i]:
active += 1
return active
def play_levels(levels):
first = levels[0]
sizex, sizey, sizez, sizew = len(first[0][0]), len(first[0]), len(first), len(levels)
new_levels = []
for w in range(-1, sizew + 1):
cube = []
for z in range(-1, sizez + 1):
surface = []
for y in range(-1, sizey + 1):
row = []
for x in range(-1, sizex +1):
cur = False
if in_level(x, y, z, w, sizex, sizey, sizez, sizew):
cur = levels[w][z][y][x]
neigh = active_neighbours(x, y, z, w, sizex, sizey, sizez, sizew, levels)
if cur and neigh in (2, 3):
row.append(True)
elif not cur and neigh == 3:
row.append(True)
else:
row.append(False)
surface.append(row)
cube.append(surface)
new_levels.append(cube)
return new_levels
def pprint(levels):
print('----------------------')
for w, cube in enumerate(levels):
print(f'{w=}')
for z, surface in enumerate(cube):
print(f'{z=}')
for row in surface:
print(''.join('#' if el else '.' for el in row))
print(' ')
def total_active(levels):
active = 0
for cube in levels:
for surface in cube:
for row in surface:
for el in row:
if el:
active += 1
return active
filename = 'small_input.txt'
filename = 'input.txt' # 1728
initial = []
with open(filename) as f:
for line in f:
initial.append([True if el == '#' else False for el in line.strip()])
cur = [[initial]]
pprint(cur)
steps = 6
for i in range(steps):
cur = play_levels(cur)
#pprint(cur)
print(total_active(cur))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment