diff --git a/aoc.py b/aoc.py index bd177fd..4f90fc9 100644 --- a/aoc.py +++ b/aoc.py @@ -3,6 +3,7 @@ from pathlib import Path from typing import Any, Generic, TypeVar from urllib.request import Request, urlopen from html.parser import HTMLParser +from importlib import import_module T1 = TypeVar("T1") T2 = TypeVar("T2") @@ -84,34 +85,30 @@ class Aoc2(ABC, Generic[T1, T2]): code_extractor.feed(site) return code_extractor.code - def run_example_1(self, ex_nr: int = 0) -> None: + def run_example_1(self, ex_nr: int = 0) -> str: inpt = self.get_examples_for_day()[ex_nr] - print(self.part1(self.parseinput1(inpt))) + return str(self.part1(self.parseinput1(inpt))) - def run_example_2(self, ex_nr: int = -1) -> None: + def run_example_2(self, ex_nr: int = -1) -> str: inpt = self.get_examples_for_day()[ex_nr] - print(self.part2(self.parseinput2(inpt))) + return str(self.part2(self.parseinput2(inpt))) - def run_1(self) -> None: + def run_1(self) -> str: inpt = self.get_data_for_day() - print(self.part1(self.parseinput1(inpt))) + return str(self.part1(self.parseinput1(inpt))) - def run_2(self) -> None: + def run_2(self) -> str: inpt = self.get_data_for_day() - print(self.part2(self.parseinput2(inpt))) + return str(self.part2(self.parseinput2(inpt))) def run(self) -> None: - print("Examples:") - print("Part 1: ", end="") - self.run_example_1() - print("Part 2: ", end="") - self.run_example_2() + print("Example:\t", end="") + print(f"{self.run_example_1()}\t", end="") + print(f"{self.run_example_2()}") - print("\nPuzzle:") - print("Part 1: ", end="") - self.run_1() - print("Part 2: ", end="") - self.run_2() + print("Real input:\t", end="") + print(f"{self.run_1()}\t", end="") + print(f"{self.run_2()}") class Aoc(Generic[T1], Aoc2[T1, T1]): @@ -126,3 +123,19 @@ class Aoc(Generic[T1], Aoc2[T1, T1]): def part2(self, inpt: T1) -> Any: return self.part1(inpt) + + +def main(): + days = [ + import_module(file.stem) + for file in Path(".").iterdir() + if file.name.endswith(".py") and file.name.startswith("day") + ] + for day in days: + print(f"Running {day.__name__}") + day.main() + print() + + +if __name__ == "__main__": + main() diff --git a/day01.py b/day01.py index 1c293e2..22549e5 100644 --- a/day01.py +++ b/day01.py @@ -1,10 +1,6 @@ import aoc -def rstr(i: str) -> str: - return "".join(reversed(i)) - - class Day01(aoc.Aoc[list[str]]): def __init__(self) -> None: super().__init__(2023, 1)