diff --git a/day17/day17.py b/day17/day17.py index 89caaf8..e205dac 100644 --- a/day17/day17.py +++ b/day17/day17.py @@ -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(): diff --git a/day17/input.txt b/day17/input.txt index e69de29..d7c4386 100644 --- a/day17/input.txt +++ b/day17/input.txt @@ -0,0 +1,8 @@ +####.#.. +.......# +#..##### +.....##. +##...### +#..#.#.# +.##...#. +#...##.. diff --git a/day18/day18.py b/day18/day18.py index 89caaf8..3d062cc 100644 --- a/day18/day18.py +++ b/day18/day18.py @@ -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(): - file = open("../day3/input.txt") - print(f"Part 1:") + file = open("input_test.txt") + 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(): diff --git a/main.py b/main.py deleted file mode 100644 index e69de29..0000000