diff --git a/aoclib/inputs.py b/aoclib/inputs.py index 34f73ef..b28097e 100644 --- a/aoclib/inputs.py +++ b/aoclib/inputs.py @@ -4,12 +4,17 @@ BASE_PATH = os.path.dirname(os.path.dirname(__file__)) INPUTS_PATH = os.path.join(BASE_PATH, 'inputs') -def getInputLineAsArray(day, return_type=None): +def getInputLineAsArray(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 """ - with open(os.path.join(INPUTS_PATH, str(day)), "r") as f: + 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.readlines() if return_type: diff --git a/aoclib/output.py b/aoclib/output.py index d7a9c78..57e6296 100644 --- a/aoclib/output.py +++ b/aoclib/output.py @@ -1,2 +1,5 @@ -def printSolution(day, part, solution): +def printSolution(day, part, solution, test=False): + if test: + print("(TEST) ", end="") + print(f"Solution to day {day}, part {part}: {solution}") diff --git a/day01.py b/day01.py index c53f2f5..273757b 100644 --- a/day01.py +++ b/day01.py @@ -1,14 +1,16 @@ import aoclib -# my_input = [1721, 979, 366, 299, 675, 1456] # debug input -my_input = aoclib.getInputLineAsArray(1, int) +if 'TEST' not in globals(): + TEST = False + +my_input = aoclib.getInputLineAsArray(day=1, return_type=int, test=TEST) globalBreak = False while len(my_input) > 0: try_value = my_input.pop() for value in my_input: if try_value + value == 2020: - aoclib.printSolution(1, 1, try_value * value) + aoclib.printSolution(1, 1, try_value * value, test=TEST) globalBreak = True break diff --git a/day01_2.py b/day01_2.py index a0a4234..2e09da6 100644 --- a/day01_2.py +++ b/day01_2.py @@ -1,7 +1,9 @@ import aoclib -# my_input = [1721, 979, 366, 299, 675, 1456] # debug input -my_input = aoclib.getInputLineAsArray(1, int) +if 'TEST' not in globals(): + TEST = False + +my_input = aoclib.getInputLineAsArray(1, int, test=TEST) globalBreak = False while len(my_input) > 0: @@ -11,7 +13,7 @@ while len(my_input) > 0: if value_1 == value_2: continue if try_value + value_1 + value_2 == 2020: - aoclib.printSolution(1, 2, try_value * value_1 * value_2) + aoclib.printSolution(1, 2, try_value * value_1 * value_2, test=TEST) globalBreak = True break diff --git a/inputs/1_test b/inputs/1_test new file mode 100644 index 0000000..e3fb011 --- /dev/null +++ b/inputs/1_test @@ -0,0 +1,6 @@ +1721 +979 +366 +299 +675 +1456 diff --git a/main.py b/main.py old mode 100644 new mode 100755 index 1cfbb36..146e01b --- a/main.py +++ b/main.py @@ -1,9 +1,29 @@ +#!/usr/bin/env python3 + +import argparse import aoclib import os +argument_parser = argparse.ArgumentParser() +argument_parser.add_argument("day", help="specify day to process; leave empty for ALL days") +argument_parser.add_argument("-t", "--test", help="also run test cases", action="store_true", default=False) +argument_parser.add_argument("-d", "--debug", help="turn on debug mode", action="store_true", default=False) +flags = argument_parser.parse_args() + +DEBUG = flags.debug + +day = "" +if flags.day: + day = "%02d" % int(flags.day) + for _, _, files in os.walk(aoclib.BASE_PATH): for f in files: - if f.startswith('day') and f.endswith('.py'): + if f.startswith('day' + day) and f.endswith('.py'): + if flags.test: + TEST = True + exec(open(f).read()) + + TEST = False exec(open(f).read()) break