commit b50168fd3be53f56a5b666963cfc1d6997c20ede Author: Stefan Harmuth Date: Fri Nov 26 08:10:55 2021 +0100 prep 2019 + day01 solve diff --git a/day01.py b/day01.py new file mode 100644 index 0000000..275419a --- /dev/null +++ b/day01.py @@ -0,0 +1,23 @@ +from aoc import AOCDay + + +class Day(AOCDay): + test_solutions_p1 = [2, 2, 654, 33583] + test_solutions_p2 = [2, 966, 50346] + + def part1(self): + fuel_sum = 0 + for x in self.getInputListAsType(int): + fuel_sum += x // 3 - 2 + + return fuel_sum + + def part2(self): + fuel_sum = 0 + for x in self.getInputListAsType(int): + fuel_add = x // 3 - 2 + while fuel_add > 0: + fuel_sum += fuel_add + fuel_add = fuel_add // 3 - 2 + + return fuel_sum diff --git a/inputs/input01 b/inputs/input01 new file mode 100644 index 0000000..eccd2e3 --- /dev/null +++ b/inputs/input01 @@ -0,0 +1,100 @@ +54032 +64433 +71758 +133884 +76994 +99596 +90491 +89188 +142280 +127352 +62127 +79849 +96049 +56527 +148029 +81386 +149827 +105377 +91970 +98708 +88611 +99785 +99229 +88460 +80396 +70097 +91784 +81733 +75671 +106787 +77196 +132234 +98698 +115243 +119574 +142851 +58964 +137814 +127695 +92139 +106277 +51240 +121351 +78316 +129472 +65201 +116068 +72803 +52582 +135433 +87619 +68096 +116952 +106437 +70517 +69840 +89863 +134618 +83823 +113436 +103779 +134819 +107928 +138503 +82509 +90104 +98001 +76202 +136238 +66426 +74030 +55075 +124163 +57133 +79908 +109977 +66903 +125400 +130961 +149293 +99203 +120307 +142403 +50262 +52854 +70851 +142213 +77567 +149144 +144582 +58138 +61765 +116209 +128192 +137436 +101406 +69037 +107389 +112389 +124402 diff --git a/inputs/test_input01_1_0 b/inputs/test_input01_1_0 new file mode 100644 index 0000000..48082f7 --- /dev/null +++ b/inputs/test_input01_1_0 @@ -0,0 +1 @@ +12 diff --git a/inputs/test_input01_1_1 b/inputs/test_input01_1_1 new file mode 100644 index 0000000..8351c19 --- /dev/null +++ b/inputs/test_input01_1_1 @@ -0,0 +1 @@ +14 diff --git a/inputs/test_input01_1_2 b/inputs/test_input01_1_2 new file mode 100644 index 0000000..ec3cc3d --- /dev/null +++ b/inputs/test_input01_1_2 @@ -0,0 +1 @@ +1969 diff --git a/inputs/test_input01_1_3 b/inputs/test_input01_1_3 new file mode 100644 index 0000000..ff999fc --- /dev/null +++ b/inputs/test_input01_1_3 @@ -0,0 +1 @@ +100756 diff --git a/inputs/test_input01_2_0 b/inputs/test_input01_2_0 new file mode 100644 index 0000000..8351c19 --- /dev/null +++ b/inputs/test_input01_2_0 @@ -0,0 +1 @@ +14 diff --git a/inputs/test_input01_2_1 b/inputs/test_input01_2_1 new file mode 100644 index 0000000..ec3cc3d --- /dev/null +++ b/inputs/test_input01_2_1 @@ -0,0 +1 @@ +1969 diff --git a/inputs/test_input01_2_2 b/inputs/test_input01_2_2 new file mode 100644 index 0000000..ff999fc --- /dev/null +++ b/inputs/test_input01_2_2 @@ -0,0 +1 @@ +100756 diff --git a/main.py b/main.py new file mode 100644 index 0000000..dbad1a8 --- /dev/null +++ b/main.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +import aoc +import argparse +import importlib +import os +import sys +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(aoc.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:]) + day_class = getattr(globals()[lib], "Day")(day) + if not flags.test: + if not flags.part or flags.part == 1: + if not day_class.test_part1(silent=True): + print("TEST FAILED! Aborting.") + sys.exit(1) + + if not flags.part or flags.part == 2: + if not day_class.test_part2(silent=True): + print("TEST FAILED! Aborting.") + sys.exit(1) + + if not flags.part or flags.part == 1: + if not flags.timeit: + if flags.test: + day_class.test_part1() + else: + aoc.printSolution(day, 1, day_class.part1()) + else: + exec_time = timeit.timeit( + 'day_class.part1()', + globals=globals(), + number=flags.timeit_number + ) / flags.timeit_number + aoc.printSolution(day, 1, day_class.part1(), exec_time=exec_time) + + if not flags.part or flags.part == 2: + if not flags.timeit: + if flags.test: + day_class.test_part2() + else: + aoc.printSolution(day, 2, day_class.part2()) + else: + exec_time = timeit.timeit( + 'day_class.part2()', + globals=globals(), + number=flags.timeit_number + ) / flags.timeit_number + aoc.printSolution(day, 2, day_class.part2(), exec_time=exec_time) diff --git a/skel_day.py b/skel_day.py new file mode 100644 index 0000000..82bab2d --- /dev/null +++ b/skel_day.py @@ -0,0 +1,13 @@ +from aoc import AOCDay +from typing import Any + + +class Day(AOCDay): + test_solutions_p1 = [] + test_solutions_p2 = [] + + def part1(self) -> Any: + return "" + + def part2(self) -> Any: + return ""