63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
import itertools
|
|
|
|
cycles = 6
|
|
initial_size = -1
|
|
max_size_xy = -1
|
|
max_size_z = -1
|
|
|
|
def read_input_boxes():
|
|
global initial_size, max_size_xy, max_size_z
|
|
with open('day17/input') as reader:
|
|
lines = reader.read().splitlines()
|
|
|
|
initial_size = len(lines[0])
|
|
max_size_xy = initial_size + (cycles * 2)
|
|
max_size_z = ((cycles * 2)+1)
|
|
|
|
initial_boxes = [[[False for _ in range(max_size_z)] for _ in range(max_size_xy)] for _ in range(max_size_xy)]
|
|
for x in range(0, initial_size):
|
|
for y in range(0, initial_size):
|
|
initial_boxes[x + cycles][y+cycles][cycles] = lines[x][y] == '#'
|
|
|
|
return initial_boxes
|
|
|
|
|
|
def is_active(boxes, location):
|
|
global initial_size, max_size_xy, max_size_z
|
|
if any([c < 0 for c in location]) or location[0]>=max_size_xy or location[1]>=max_size_xy or location[2]>=max_size_z:
|
|
return 0
|
|
return 1 if boxes[location[0]][location[1]][location[2]] else 0
|
|
|
|
|
|
def generate_all_neighbours(x, y, z):
|
|
x_checks = [x, x - 1, x + 1]
|
|
y_checks = [y, y - 1, y + 1]
|
|
z_checks = [z, z - 1, z + 1]
|
|
neighbours = list(itertools.product(x_checks, y_checks, z_checks))
|
|
neighbours.remove((x, y, z))
|
|
return neighbours
|
|
|
|
|
|
def check_state(boxes, previous_boxes, x, y, z):
|
|
neighbours = generate_all_neighbours(x, y, z)
|
|
active_neighbours = sum(map(lambda n: is_active(previous_boxes, n), neighbours))
|
|
if boxes[x][y][z]:
|
|
boxes[x][y][z] = 2 <= active_neighbours <= 3
|
|
else:
|
|
boxes[x][y][z] = active_neighbours == 3
|
|
|
|
|
|
boxes = read_input_boxes()
|
|
|
|
|
|
for c in range(cycles):
|
|
previous_boxes = [[[boxes[x][y][z] for z in range(max_size_z)] for y in range(max_size_xy)] for x in range(max_size_xy)]
|
|
for x in range(max_size_xy):
|
|
for y in range(max_size_xy):
|
|
for z in range(max_size_z):
|
|
check_state(boxes, previous_boxes, x, y, z)
|
|
|
|
res = sum([sum([sum(y) for y in x]) for x in boxes])
|
|
|
|
print(res)
|