This commit is contained in:
2020-12-28 18:45:07 +01:00
parent b7489ef1ce
commit 2cdc62f52e
4 changed files with 116 additions and 4 deletions

View File

@@ -1,6 +1,74 @@
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("../day3/input.txt")
print(f"Part 1:")
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():

View File

@@ -0,0 +1,8 @@
####.#..
.......#
#..#####
.....##.
##...###
#..#.#.#
.##...#.
#...##..