aoc2021/aoclib/day.py
2021-11-21 13:04:32 +01:00

113 lines
3.5 KiB
Python

import os
from typing import List, Any
from .tools import splitLine
from .output import printSolution
BASE_PATH = os.path.dirname(os.path.dirname(__file__))
INPUTS_PATH = os.path.join(BASE_PATH, 'inputs')
class AOCDay:
day: int
input: List # our input is always a list of str/lines
test_solutions_p1: List
test_solutions_p2: List
def __init__(self, day):
self.day = day
with open(os.path.join(INPUTS_PATH, "input%02d" % day)) as f:
self.input = f.read().splitlines()
def part1(self) -> Any:
pass
def part2(self) -> Any:
pass
def test_part1(self) -> bool:
live_input = self.input.copy()
for case, solution in enumerate(self.test_solutions_p1):
with open(os.path.join(INPUTS_PATH, "test_input%02d_1_%d" % (self.day, case))) as f:
self.input = f.read().splitlines()
check = self.part1()
printSolution(self.day, 1, check, solution, case)
if check != solution:
return False
self.input = live_input
return True
def test_part2(self) -> bool:
live_input = self.input.copy()
for case, solution in enumerate(self.test_solutions_p2):
with open(os.path.join(INPUTS_PATH, "test_input%02d_2_%d" % (self.day, case))) as f:
self.input = f.read().splitlines()
check = self.part2()
printSolution(self.day, 2, check, solution, case)
if check != solution:
return False
self.input = live_input
return True
def getInputListAsType(self, return_type):
"""
get input as list casted to return_type, each line representing one list entry
"""
return [return_type(i) for i in self.input]
def getMultiLineInputAsArray(self, return_type=None, join_char=None):
"""
get input for day x as 2d array, split by empty lines
"""
lines = self.input
lines.append('')
return_array = []
line_array = []
for line in lines:
if not line:
if join_char:
return_array.append(join_char.join(line_array))
else:
return_array.append(line_array)
line_array = []
continue
if return_type:
line_array.append(return_type(line))
else:
line_array.append(line)
return return_array
def getInputAs2DArray(self, return_type=None):
"""
get input for day x as 2d-array (a[line][pos])
"""
if return_type is None:
return self.input # strings already act like a list
else:
return_array = []
for line in self.input:
return_array.append([return_type(i) for i in line])
return return_array
def getInputAsArraySplit(self, split_char=',', return_type=None):
"""
get input for day x with the lines split by split_char
if input has only one line, returns a 1d array with the values
if input has multiple lines, returns a 2d array (a[line][values])
"""
if len(self.input) == 1:
return splitLine(line=self.input[0], split_char=split_char, return_type=return_type)
else:
return_array = []
for line in self.input:
return_array.append(splitLine(line=line, split_char=split_char, return_type=return_type))
return return_array