aoc2020/main.py
2020-12-20 10:18:12 +01:00

88 lines
3.2 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import aoclib
import importlib
import os
import timeit
TIMEIT_NUMBER = 50
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument("-d", "--day", help="specify day to process; leave empty for ALL days", type=int)
argument_parser.add_argument("-t", "--test", help="run test cases", action="store_true", default=False)
argument_parser.add_argument("-p", "--part", help="run only part x", choices=[1, 2], type=int)
argument_parser.add_argument("--timeit", help="measure execution time", action="store_true", default=False)
argument_parser.add_argument(
"--timeit-number",
help="build average time over this many executions",
type=int,
default=TIMEIT_NUMBER
)
flags = argument_parser.parse_args()
import_day = ""
if flags.day:
import_day = "%02d" % flags.day
imported = []
for _, _, files in os.walk(aoclib.BASE_PATH):
for f in files:
if f.startswith('day' + import_day) and f.endswith('.py'):
lib_name = f[:-3]
globals()[lib_name] = importlib.import_module(lib_name)
imported.append(lib_name)
break
for lib in sorted(imported):
day = int(lib[-2:])
if not flags.test:
if not flags.part or flags.part == 1:
test_part_1 = getattr(globals()[lib], "part1")(test_mode=True)
test_solution_1 = getattr(globals()[lib], "TEST_SOLUTION_PART1")
if test_part_1 != test_solution_1:
print(
"TEST FAILED for Day %s Part 1: Expected %s; Got %s"
% (day, test_solution_1, test_part_1)
)
if not flags.part or flags.part == 2:
test_part_2 = getattr(globals()[lib], "part2")(test_mode=True)
test_solution_2 = getattr(globals()[lib], "TEST_SOLUTION_PART2")
if test_part_2 != test_solution_2:
print(
"TEST FAILED for Day %s Part 2: Expected %s; Got %s"
% (day, test_solution_2, test_part_2)
)
if not flags.part or flags.part == 1:
if not flags.timeit:
solution = globals()[lib].part1(test_mode=flags.test)
if flags.test:
aoclib.printSolution(day, 1, solution, test=globals()[lib].TEST_SOLUTION_PART1)
else:
aoclib.printSolution(day, 1, solution)
else:
exec_time = timeit.timeit(
'globals()[lib].part1(test_mode=flags.test)',
globals=globals(),
number=flags.timeit_number
) / flags.timeit_number
aoclib.print_execution_time(day, 1, exec_time)
if not flags.part or flags.part == 2:
if not flags.timeit:
solution = globals()[lib].part2(test_mode=flags.test)
if flags.test:
aoclib.printSolution(day, 2, solution, test=globals()[lib].TEST_SOLUTION_PART2)
else:
aoclib.printSolution(day, 2, solution)
else:
exec_time = timeit.timeit(
'globals()[lib].part2(test_mode=flags.test)',
globals=globals(),
number=flags.timeit_number
) / flags.timeit_number
aoclib.print_execution_time(day, 2, exec_time)