more input process options

This commit is contained in:
Stefan Harmuth 2020-12-03 09:57:10 +01:00
parent 95e56b9333
commit fcd394aaba
4 changed files with 50 additions and 7 deletions

View File

@ -4,7 +4,7 @@ BASE_PATH = os.path.dirname(os.path.dirname(__file__))
INPUTS_PATH = os.path.join(BASE_PATH, 'inputs')
def getInputLineAsArray(day, return_type=None, test=False):
def getInputAsArray(day, return_type=None, test=False):
"""
get input for day x as array, each line representing one array entry
when type is given, each entry will be converted to the specified type
@ -21,3 +21,46 @@ def getInputLineAsArray(day, return_type=None, test=False):
return [return_type(i) for i in input_array]
else:
return input_array
def getInputAs2DArray(day, return_type=None, test=False):
"""
get input for day x as 2d-array (a[line][pos])
"""
lines = getInputAsArray(day=day, test=test)
if return_type is None:
return lines # strings already act like an array
else:
return_array = []
for line in lines:
return_array.append([return_type(i) for i in line])
return return_array
def splitLine(line, split_char=',', return_type=None):
if split_char:
line = line.split(split_char)
if return_type is None:
return line
else:
return [return_type(i) for i in line]
def getInputAsArraySplit(day, split_char=',', return_type=None, test=False):
"""
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])
"""
lines = getInputAsArray(day=day, test=test)
if len(lines) == 1:
return splitLine(line=lines[0], split_char=split_char, return_type=return_type)
else:
return_array = []
for line in lines:
return_array.append(splitLine(line=line, split_char=split_char, return_type=return_type))
return return_array

View File

@ -16,12 +16,12 @@ def getProductOf2020Sum(input_lines, combinations):
def part1(test_mode=False):
my_input = aoclib.getInputLineAsArray(day=1, return_type=int, test=test_mode)
my_input = aoclib.getInputAsArray(day=1, return_type=int, test=test_mode)
return getProductOf2020Sum(my_input, 2)
def part2(test_mode=False):
my_input = aoclib.getInputLineAsArray(day=1, return_type=int, test=test_mode)
my_input = aoclib.getInputAsArray(day=1, return_type=int, test=test_mode)
return getProductOf2020Sum(my_input, 3)

View File

@ -7,7 +7,7 @@ splitter = re.compile(r'([0-9]+)-([0-9]+) ([a-z]): (.*)')
def part1(test_mode=False):
my_input = aoclib.getInputLineAsArray(day=2, test=test_mode)
my_input = aoclib.getInputAsArray(day=2, test=test_mode)
valid_count = 0
for line in my_input:
match = re.match(splitter, line)
@ -19,7 +19,7 @@ def part1(test_mode=False):
def part2(test_mode=False):
my_input = aoclib.getInputLineAsArray(day=2, test=test_mode)
my_input = aoclib.getInputAsArray(day=2, test=test_mode)
valid_count = 0
for line in my_input:
match = re.match(splitter, line)

View File

@ -26,12 +26,12 @@ def check_for_trees(right_step, down_step, treelines):
def part1(test_mode=False):
my_input = aoclib.getInputLineAsArray(day=3, test=test_mode)
my_input = aoclib.getInputAsArray(day=3, test=test_mode)
return check_for_trees(3, 1, my_input)
def part2(test_mode=False):
my_input = aoclib.getInputLineAsArray(day=3, test=test_mode)
my_input = aoclib.getInputAsArray(day=3, test=test_mode)
tree_count1 = check_for_trees(1, 1, my_input)
tree_count2 = check_for_trees(3, 1, my_input)
tree_count3 = check_for_trees(5, 1, my_input)