dinges
This commit is contained in:
@@ -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():
|
def part1():
|
||||||
file = open("../day3/input.txt")
|
file = open("../day17/input_test.txt")
|
||||||
print(f"Part 1:")
|
|
||||||
|
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():
|
def part2():
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
####.#..
|
||||||
|
.......#
|
||||||
|
#..#####
|
||||||
|
.....##.
|
||||||
|
##...###
|
||||||
|
#..#.#.#
|
||||||
|
.##...#.
|
||||||
|
#...##..
|
||||||
|
|||||||
@@ -1,6 +1,42 @@
|
|||||||
|
import numpy as np
|
||||||
|
|
||||||
|
def solve(index, line):
|
||||||
|
val = 0
|
||||||
|
|
||||||
|
while index < len(line):
|
||||||
|
|
||||||
|
print(index)
|
||||||
|
|
||||||
|
if line[index] == '(':
|
||||||
|
val += solve(index + 1, line)
|
||||||
|
elif line[index] == ')':
|
||||||
|
return val
|
||||||
|
|
||||||
|
elif line[index] == '+':
|
||||||
|
if line[index + 1] == '(':
|
||||||
|
val += solve(index + 2, line)
|
||||||
|
else:
|
||||||
|
val += int(line[index + 1])
|
||||||
|
elif line[index] == '*':
|
||||||
|
val *= solve(index + 1, line)
|
||||||
|
|
||||||
|
else:
|
||||||
|
val += line[index]
|
||||||
|
|
||||||
|
index += 1
|
||||||
|
|
||||||
|
return val
|
||||||
|
|
||||||
|
|
||||||
def part1():
|
def part1():
|
||||||
file = open("../day3/input.txt")
|
file = open("input_test.txt")
|
||||||
print(f"Part 1:")
|
lines = [line.strip() for line in file.readlines()]
|
||||||
|
|
||||||
|
sum = 0
|
||||||
|
for line in lines:
|
||||||
|
sum += solve(0, line.split(' '))
|
||||||
|
|
||||||
|
print(f"Part 1: {sum}")
|
||||||
|
|
||||||
|
|
||||||
def part2():
|
def part2():
|
||||||
|
|||||||
Reference in New Issue
Block a user