82 lines
2.2 KiB
Python
82 lines
2.2 KiB
Python
import copy
|
|
|
|
|
|
def count(box):
|
|
count = 0
|
|
size = len(box)
|
|
for x in range(0, size):
|
|
for y in range(0, size):
|
|
for z in range(0, size):
|
|
if box[z][x][y] == '#':
|
|
count += 1
|
|
|
|
return count
|
|
|
|
|
|
def simulate(box):
|
|
size = len(box)
|
|
forbidden = [-1, size]
|
|
|
|
next_box = [[['' for _ in range(size)] for _ in range(size)] for _ in range(size)]
|
|
|
|
for z in range(0, size):
|
|
for x in range(0, size):
|
|
for y in range(0, size):
|
|
|
|
xs = [a for a in [x - 1, x, x + 1] if a not in forbidden]
|
|
ys = [a for a in [y - 1, y, y + 1] if a not in forbidden]
|
|
zs = [a for a in [z - 1, z, z + 1] if a not in forbidden]
|
|
|
|
count = 0
|
|
|
|
for xn in xs:
|
|
for yn in ys:
|
|
for zn in zs:
|
|
if box[zn][xn][yn] == '#':
|
|
count += 1
|
|
|
|
if box[z][x][y] == '#':
|
|
if 2 == count or 3 == count:
|
|
next_box[z][x][y] = '#'
|
|
else:
|
|
next_box[z][x][y] = '.'
|
|
elif box[z][x][y] == '.':
|
|
if count == 3:
|
|
next_box[z][x][y] = '#'
|
|
else:
|
|
next_box[z][x][y] = '.'
|
|
|
|
return next_box
|
|
|
|
|
|
def part1():
|
|
file = open("../day17/input_test.txt")
|
|
|
|
lines = [line.strip() for line in file.readlines()]
|
|
|
|
simulations = 6
|
|
size = len(lines) + (2 * simulations)
|
|
|
|
box = [[['.' for _ in range(size)] for _ in range(size)] for _ in range(size)]
|
|
|
|
for x in range(0, len(lines)):
|
|
for y in range(0, len(lines)):
|
|
box[0 + simulations][x + simulations][y + simulations] = lines[x][y]
|
|
box[1 + simulations][x + simulations][y + simulations] = lines[x][y]
|
|
box[2 + simulations][x + simulations][y + simulations] = lines[x][y]
|
|
|
|
for i in range(0, simulations):
|
|
box = simulate(box)
|
|
|
|
print(f"Part 1: {count(box)}")
|
|
|
|
|
|
def part2():
|
|
file = open("../day10/input_test.txt")
|
|
print(f"Part 2:")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
part1()
|
|
part2()
|