You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
3.8 KiB
122 lines
3.8 KiB
from copy import deepcopy
|
|
from termcolor import colored
|
|
|
|
|
|
def show(array):
|
|
for z in range(0, len(array)):
|
|
print(colored('\nz = ' + str(z), 'yellow'))
|
|
for i in range(0, len(array[z])):
|
|
print(colored(''.join(array[z][i]), 'blue'))
|
|
|
|
|
|
def show4(array):
|
|
for w in range(0, len(array)):
|
|
print(colored('\nw = ' + str(w), 'magenta'))
|
|
for z in range(0, len(array[w])):
|
|
print(colored('\nz = ' + str(z), 'yellow'))
|
|
for i in range(0, len(array[w][z])):
|
|
print(colored(''.join(array[w][z][i]), 'blue'))
|
|
|
|
|
|
def oob(space, z, y, x):
|
|
if z < 0: return True
|
|
if z >= len(space): return True
|
|
if y < 0: return True
|
|
if y >= len(space[z]): return True
|
|
if x < 0: return True
|
|
if x >= len(space[z][y]): return True
|
|
return False
|
|
|
|
|
|
def oob4(space, w, z, y, x):
|
|
if w < 0: return True
|
|
if w >= len(space): return True
|
|
if z < 0: return True
|
|
if z >= len(space[w]): return True
|
|
if y < 0: return True
|
|
if y >= len(space[w][z]): return True
|
|
if x < 0: return True
|
|
if x >= len(space[w][z][y]): return True
|
|
return False
|
|
|
|
|
|
def neighbors(space, z, y, x):
|
|
dirs = [(z, y, x) for x in [0, 1, -1] for y in [0, 1, -1] for z in [0, 1, -1]]
|
|
dirs.remove((0, 0, 0))
|
|
for d in dirs:
|
|
if not oob(space, z+d[0], y+d[1], x+d[2]):
|
|
yield space[z+d[0]][y+d[1]][x+d[2]]
|
|
|
|
|
|
def neighbors4(space, w, z, y, x):
|
|
dirs = [(w, z, y, x) for x in [0, 1, -1] for y in [0, 1, -1] for z in [0, 1, -1] for w in [0, 1, -1]]
|
|
dirs.remove((0, 0, 0, 0))
|
|
for d in dirs:
|
|
if not oob4(space, w+d[0], z+d[1], y+d[2], x+d[3]):
|
|
yield space[w+d[0]][z+d[1]][y+d[2]][x+d[3]]
|
|
|
|
|
|
def step(array):
|
|
next = deepcopy(array)
|
|
for z in range(0, len(array)):
|
|
for y in range(0, len(array[z])):
|
|
for x in range(0, len(array[z][y])):
|
|
c = [i for i in neighbors(array, z, y, x)].count('#')
|
|
if array[z][y][x] == '#' and not (c == 2 or c == 3):
|
|
next[z][y][x] = '.'
|
|
if array[z][y][x] == '.' and c == 3:
|
|
next[z][y][x] = '#'
|
|
return next
|
|
|
|
|
|
def step4(array):
|
|
next = deepcopy(array)
|
|
for w in range(0, len(array)):
|
|
for z in range(0, len(array[w])):
|
|
for y in range(0, len(array[w][z])):
|
|
for x in range(0, len(array[w][z][y])):
|
|
c = [i for i in neighbors4(array, w, z, y, x)].count('#')
|
|
if array[w][z][y][x] == '#' and not (c == 2 or c == 3):
|
|
next[w][z][y][x] = '.'
|
|
if array[w][z][y][x] == '.' and c == 3:
|
|
next[w][z][y][x] = '#'
|
|
return next
|
|
|
|
|
|
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input17") as f:
|
|
lines = f.readlines()
|
|
lines = [list(l) for l in lines]
|
|
empty = [[['.' for i in range(20)] for j in range(20)] for k in range(13)]
|
|
for i in range(0, 8):
|
|
for j in range(0, 8):
|
|
empty[6][j+6][i+6] = lines[j][i]
|
|
show(empty)
|
|
|
|
for i in range(0, 6):
|
|
print(colored('\n\nstep: ' + str(i+1) + '\n', 'red'))
|
|
empty = step(empty)
|
|
show(empty)
|
|
|
|
count = 0
|
|
for i in range(0, 13):
|
|
for j in range(0, 20):
|
|
count += empty[i][j].count('#')
|
|
print(colored(count, 'cyan'))
|
|
|
|
hypercube = [[[['.' for i in range(20)] for j in range(20)] for k in range(13)] for l in range(13)]
|
|
for i in range(0, 8):
|
|
for j in range(0, 8):
|
|
hypercube[6][6][j+6][i+6] = lines[j][i]
|
|
|
|
for i in range(0, 6):
|
|
print(colored('\n\nstep: ' + str(i + 1) + '\n', 'red'))
|
|
hypercube = step4(hypercube)
|
|
show4(hypercube)
|
|
|
|
count4 = 0
|
|
for i in range(0, 13):
|
|
for j in range(0, 13):
|
|
for k in range(0, 20):
|
|
count4 += hypercube[i][j][k].count('#')
|
|
|
|
print(colored(count4, 'cyan'))
|
|
|