Created
December 19, 2020 17:44
-
-
Save mei-li/99239ee9153bfe1a2555580e158d1eb9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def in_level(x, y, z, sizex, sizey, sizez): | |
return 0 <= x < sizex and 0 <= y < sizey and 0 <= z < sizez | |
def active_neighbours(x, y, z, sizex, sizey, sizez, levels): | |
active = 0 | |
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) == (x, y, z): | |
continue | |
if in_level(i, j, k, sizex, sizey, sizez) and levels[k][j][i]: | |
active += 1 | |
return active | |
def play_levels(levels): | |
first = levels[0] | |
sizex, sizey, sizez = len(first[0]), len(first), len(levels) | |
new_levels = [] | |
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, sizex, sizey, sizez): | |
cur = levels[z][y][x] | |
neigh = active_neighbours(x, y, z, sizex, sizey, sizez, 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) | |
new_levels.append(surface) | |
return new_levels | |
def pprint(levels): | |
print('----------------------') | |
for surface in levels: | |
for row in surface: | |
print(''.join('#' if el else '.' for el in row)) | |
print(' ') | |
def total_active(levels): | |
active = 0 | |
for surface in levels: | |
for row in surface: | |
for el in row: | |
if el: | |
active += 1 | |
return active | |
filename = 'small_input.txt' | |
filename = 'input.txt' # 215 | |
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