Failed day 17.1 (Python)
This commit is contained in:
parent
0ee5c548a7
commit
2fc5fb700c
5 changed files with 236 additions and 0 deletions
14
17-conwayCubes/info.json
Normal file
14
17-conwayCubes/info.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"year": "2020",
|
||||
"day": "17",
|
||||
"title": "Conway Cubes",
|
||||
"testCases": {
|
||||
"one": [
|
||||
{
|
||||
"input": ".#.\n..#\n###\n",
|
||||
"expected": 112
|
||||
}
|
||||
],
|
||||
"two": []
|
||||
}
|
||||
}
|
77
17-conwayCubes/python/__main__.py
Normal file
77
17-conwayCubes/python/__main__.py
Normal file
|
@ -0,0 +1,77 @@
|
|||
import json
|
||||
import platform
|
||||
import sys
|
||||
|
||||
from rich import print
|
||||
|
||||
from partOne import partOne
|
||||
from partTwo import partTwo
|
||||
|
||||
|
||||
def run_tests(test_cases):
|
||||
do_tests = True
|
||||
if len(test_cases) == 0:
|
||||
do_tests = False
|
||||
elif len(test_cases["one"]) == 0 and len(test_cases["two"]) == 0:
|
||||
do_tests = False
|
||||
|
||||
if not do_tests:
|
||||
print("Info: no test cases specified. Skipping tests\n")
|
||||
return
|
||||
|
||||
print("Test cases")
|
||||
|
||||
def rt(tcs, f, n):
|
||||
for i, tc in enumerate(tcs):
|
||||
print(f"{n}.{i+1} ", end="")
|
||||
expectedInt = tc["expected"]
|
||||
result = f(str(tc["input"]))
|
||||
if result == expectedInt:
|
||||
print("[green]pass[/green]")
|
||||
else:
|
||||
print(f"[red]fail[/red] (got {result}, expected {expectedInt})")
|
||||
|
||||
rt(test_cases["one"], partOne, 1)
|
||||
rt(test_cases["two"], partTwo, 2)
|
||||
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
info = open("info.json").read()
|
||||
except FileNotFoundError:
|
||||
print("Error: could not open info.json")
|
||||
sys.exit(-1)
|
||||
|
||||
info = json.loads(info)
|
||||
|
||||
year = info["year"]
|
||||
day = info["day"]
|
||||
title = info["title"]
|
||||
|
||||
print(f"[yellow]AoC {year}[/yellow]: day {day} - {title}")
|
||||
print(f"Python {platform.python_version()}\n")
|
||||
|
||||
try:
|
||||
challenge_input = open("input.txt").read()
|
||||
except FileNotFoundError:
|
||||
print("Error: could not open input.txt")
|
||||
sys.exit(-1)
|
||||
|
||||
if "vis" in sys.argv:
|
||||
import visualise
|
||||
|
||||
print("[green]Running visualisation....[/green]")
|
||||
|
||||
visualise.visualise(challenge_input)
|
||||
sys.exit()
|
||||
|
||||
run_tests(info["testCases"])
|
||||
|
||||
if "debug" in sys.argv:
|
||||
sys.exit()
|
||||
|
||||
print("Answers")
|
||||
print("Part 1:", partOne(challenge_input))
|
||||
print("Part 2:", partTwo(challenge_input))
|
49
17-conwayCubes/python/common.py
Normal file
49
17-conwayCubes/python/common.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
from typing import List, Tuple
|
||||
import math
|
||||
|
||||
|
||||
active = "#"
|
||||
inactive = "."
|
||||
|
||||
|
||||
class Matrix3D:
|
||||
master_array: List[List[List]]
|
||||
x_zero: int
|
||||
y_zero: int
|
||||
z_zero: int
|
||||
|
||||
def __init__(self, x, y, z, default_value=None):
|
||||
self.master_array = [[[default_value for _ in range(x)] for _ in range(y)] for _ in range(z)]
|
||||
self.x_zero = math.floor(len(self.master_array[0][0]) / 2)
|
||||
self.y_zero = math.floor(len(self.master_array[0]) / 2)
|
||||
self.z_zero = math.floor(len(self.master_array) / 2)
|
||||
|
||||
def translate(self, x:int, y:int, z:int) -> Tuple[int, int, int]:
|
||||
x += self.x_zero
|
||||
y += self.y_zero
|
||||
z += self.z_zero
|
||||
return x, y, z
|
||||
|
||||
def __str__(self) -> str:
|
||||
return str(self.master_array)
|
||||
|
||||
def __getitem__(self, location_tuple:Tuple[int, int, int]):
|
||||
x, y, z = location_tuple
|
||||
|
||||
if x < 0 or y < 0 or z < 0:
|
||||
raise IndexError("list index out of range")
|
||||
|
||||
return self.master_array[z][y][x]
|
||||
|
||||
def __setitem__(self, location_tuple:Tuple[int, int, int], value):
|
||||
x, y, z = location_tuple
|
||||
|
||||
if x < 0 or y < 0 or z < 0:
|
||||
raise IndexError("list index out of range")
|
||||
|
||||
self.master_array[z][y][x] = value
|
||||
|
||||
|
||||
def parse(instr: str) -> int:
|
||||
|
||||
return 0
|
90
17-conwayCubes/python/partOne.py
Normal file
90
17-conwayCubes/python/partOne.py
Normal file
|
@ -0,0 +1,90 @@
|
|||
from common import *
|
||||
import copy
|
||||
|
||||
|
||||
iterations = 6
|
||||
|
||||
translation_vectors = [
|
||||
(-1, 1, 1),
|
||||
(0, 1, 1),
|
||||
(1, 1, 1),
|
||||
(-1, 0, 1),
|
||||
(0, 0, 1),
|
||||
(1, 0, 1),
|
||||
(-1, -1, 1),
|
||||
(0, -1, 1),
|
||||
(1, -1, 1),
|
||||
(-1, 1, -1),
|
||||
(0, 1, -1),
|
||||
(1, 1, -1),
|
||||
(-1, 0, -1),
|
||||
(0, 0, -1),
|
||||
(1, 0, -1),
|
||||
(-1, -1, -1),
|
||||
(0, -1, -1),
|
||||
(1, -1, -1),
|
||||
(-1, 1, 0),
|
||||
(0, 1, 0),
|
||||
(1, 1, 0),
|
||||
(-1, 0, 0),
|
||||
(1, 0, 0),
|
||||
(-1, -1, 0),
|
||||
(0, -1, 0),
|
||||
(1, -1, 0)
|
||||
]
|
||||
|
||||
def find_neighbours(matrix:Matrix3D, raw_point:Tuple[int, int, int]) -> int:
|
||||
x, y, z = raw_point
|
||||
num_neighbours = 0
|
||||
for (vx, vy, vz) in translation_vectors:
|
||||
try:
|
||||
current_val = matrix[x+vx, y+vy, z+vz]
|
||||
except IndexError:
|
||||
continue
|
||||
if current_val == active:
|
||||
num_neighbours += 1
|
||||
return num_neighbours
|
||||
|
||||
def iterate(matrix:Matrix3D) -> Matrix3D:
|
||||
new = copy.deepcopy(matrix)
|
||||
sz = len(matrix.master_array)
|
||||
sy = len(matrix.master_array[0])
|
||||
sx = len(matrix.master_array[0][0])
|
||||
for z in range(sz):
|
||||
for y in range(sy):
|
||||
for x in range(sx):
|
||||
neighbours = find_neighbours(matrix, (x, y, z))
|
||||
current_state = matrix[x, y, z]
|
||||
if (neighbours == 2 or neighbours == 3) and current_state != active:
|
||||
new[x, y, z] = inactive
|
||||
elif neighbours == 3 and current_state == inactive:
|
||||
new[x, y, z] = active
|
||||
return new
|
||||
|
||||
|
||||
def partOne(instr: str) -> int:
|
||||
|
||||
input_array = [[y for y in x] for x in instr.strip().split("\n")]
|
||||
modifier = math.floor(len(input_array) / 2)
|
||||
|
||||
size = (iterations * 2) + 1 + len(input_array)
|
||||
print(size)
|
||||
|
||||
m = Matrix3D(size, size, size, default_value=".")
|
||||
for y in range(len(input_array)):
|
||||
for x in range(len(input_array[y])):
|
||||
ux, uy, uz = m.translate(x-modifier, y-modifier, 0)
|
||||
m[ux, uy, uz] = input_array[y][x]
|
||||
|
||||
for _ in range(iterations):
|
||||
m = iterate(m)
|
||||
|
||||
active_count = 0
|
||||
for z in range(len(m.master_array)):
|
||||
for y in range(len(m.master_array[0])):
|
||||
for x in range(len(m.master_array[0][0])):
|
||||
if m[x, y, z] == active:
|
||||
active_count += 1
|
||||
|
||||
return active_count
|
||||
|
6
17-conwayCubes/python/partTwo.py
Normal file
6
17-conwayCubes/python/partTwo.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from common import *
|
||||
|
||||
|
||||
def partTwo(instr: str) -> int:
|
||||
input_list = parse(instr)
|
||||
return 0
|
Loading…
Add table
Add a link
Reference in a new issue