From fcd394aaba7112d453accfd9f581a2ee32756720 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Thu, 3 Dec 2020 09:57:10 +0100 Subject: [PATCH] more input process options --- aoclib/inputs.py | 45 ++++++++++++++++++++++++++++++++++++++++++++- day01.py | 4 ++-- day02.py | 4 ++-- day03.py | 4 ++-- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/aoclib/inputs.py b/aoclib/inputs.py index 2133778..9886377 100644 --- a/aoclib/inputs.py +++ b/aoclib/inputs.py @@ -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 diff --git a/day01.py b/day01.py index 6c0f01c..b045880 100755 --- a/day01.py +++ b/day01.py @@ -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) diff --git a/day02.py b/day02.py index 416f57a..08c215b 100755 --- a/day02.py +++ b/day02.py @@ -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) diff --git a/day03.py b/day03.py index b06d228..4b8bd99 100644 --- a/day03.py +++ b/day03.py @@ -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)