67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
import os
|
|
|
|
BASE_PATH = os.path.dirname(os.path.dirname(__file__))
|
|
INPUTS_PATH = os.path.join(BASE_PATH, 'inputs')
|
|
|
|
|
|
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
|
|
"""
|
|
if test:
|
|
file = os.path.join(INPUTS_PATH, str(day) + "_test")
|
|
else:
|
|
file = os.path.join(INPUTS_PATH, str(day))
|
|
|
|
with open(file, "r") as f:
|
|
input_array = f.read().splitlines()
|
|
|
|
if return_type:
|
|
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
|