|
1 | 1 | import abc |
2 | | -import logging |
3 | 2 | from pathlib import Path |
4 | | -from typing import Any |
| 3 | +from typing import Any, ClassVar |
5 | 4 |
|
6 | | -import __main__ |
| 5 | +from typing_extensions import Protocol |
| 6 | + |
| 7 | +from aoc.input_providers import InputProvider |
7 | 8 |
|
8 | 9 | REPO_ROOT = Path(__file__).parent.parent.parent |
9 | | -logger = logging.getLogger(__name__) |
10 | 10 |
|
11 | 11 |
|
12 | | -class BaseChallenge(abc.ABC): |
| 12 | +def get_day_from_module(module_name: str) -> int: |
| 13 | + """Return the day of this challenge based on the module name.""" |
| 14 | + return int(module_name.split(".")[-1].split("_")[1]) |
| 15 | + |
| 16 | + |
| 17 | +class ChallengeProtocol(Protocol): |
| 18 | + def part_1(self, input_lines: list[str]) -> Any: |
| 19 | + ... |
| 20 | + |
| 21 | + def part_2(self, input_lines: list[str]) -> Any: |
| 22 | + ... |
| 23 | + |
| 24 | + |
| 25 | +class BaseChallenge(ChallengeProtocol, abc.ABC): |
13 | 26 | """Base class for all challenges.""" |
14 | 27 |
|
15 | | - def __init__(self, use_test_data: bool = False, data_dir: Path | None = None): |
16 | | - self._use_test_data = use_test_data |
| 28 | + day: ClassVar[int] |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + input_provider: InputProvider, |
| 33 | + ): |
| 34 | + self._input_provider = input_provider |
17 | 35 | self._input_lines: dict[int | None, list[str]] = {} |
18 | | - self._data_dir = data_dir or REPO_ROOT.joinpath("data") |
19 | 36 |
|
20 | | - @property |
21 | | - def day(self) -> int: |
| 37 | + def __init_subclass__(cls, **kwargs): |
| 38 | + cls.day = cls._get_day() |
| 39 | + |
| 40 | + @classmethod |
| 41 | + def _get_day(cls) -> int: |
| 42 | + import __main__ |
| 43 | + |
22 | 44 | """Return the day of this challenge based on the module name.""" |
23 | | - if self.__module__ == "__main__": # challenge is run directly |
| 45 | + if cls.__module__ == "__main__": # challenge is run directly |
24 | 46 | return int(Path(__main__.__file__).parent.name.split("_")[1]) |
25 | | - return int(self.__module__.split(".")[-1].split("_")[1]) |
26 | | - |
27 | | - def get_input_filename(self, part: int | None = None) -> str: |
28 | | - """Return the input filename for this challenge.""" |
29 | | - base_filename = ( |
30 | | - f"{self.day:02}_test_input" |
31 | | - if self._use_test_data |
32 | | - else f"{self.day:02}_input" |
33 | | - ) |
34 | | - default_filename = f"{base_filename}.txt" |
35 | | - if part is not None: |
36 | | - filename = f"{base_filename}_part_{part}.txt" |
37 | | - if self._data_dir.joinpath(filename).exists(): |
38 | | - return filename |
39 | | - else: |
40 | | - logger.info( |
41 | | - "File %s does not exist. Using default instead %s", |
42 | | - filename, |
43 | | - default_filename, |
44 | | - ) |
45 | | - return default_filename |
46 | | - |
47 | | - def get_input_file_path(self, filename: str) -> Path: |
48 | | - """Return the input filename for this challenge.""" |
49 | | - return self._data_dir.joinpath(filename) |
| 47 | + return get_day_from_module(cls.__module__) |
50 | 48 |
|
51 | 49 | def get_input_lines(self, part: int | None = None) -> list[str]: |
52 | 50 | """Return the input lines for this challenge. Relative to this file""" |
53 | | - filename = self.get_input_filename(part) |
54 | | - print(f"Using data from {filename}") |
55 | 51 | if not self._input_lines.get(part): |
56 | 52 | self._input_lines[part] = ( |
57 | | - self.get_input_file_path(filename).read_text().splitlines() |
| 53 | + self._input_provider.provide_input(part).strip().split("\n") |
58 | 54 | ) |
59 | 55 | return self._input_lines[part] |
60 | 56 |
|
61 | 57 | def set_input_lines(self, lines: list[str], part: int | None = None): |
62 | 58 | self._input_lines[part] = lines |
63 | 59 |
|
64 | 60 | @abc.abstractmethod |
65 | | - def part_1(self) -> Any: |
| 61 | + def part_1(self, input_lines: list[str]) -> Any: |
66 | 62 | """Return the solution for part 1 of this challenge.""" |
67 | 63 | ... |
68 | 64 |
|
69 | 65 | @abc.abstractmethod |
70 | | - def part_2(self) -> Any: |
| 66 | + def part_2(self, input_lines: list[str]) -> Any: |
71 | 67 | """Return the solution for part 2 of this challenge.""" |
72 | 68 | ... |
73 | 69 |
|
74 | 70 | def solve(self) -> tuple[Any, Any]: |
75 | 71 | """Return solutions for this challenge as a 2 element tuple.""" |
76 | | - return self.part_1(), self.part_2() |
| 72 | + return self.part_1(self.get_input_lines(part=1)), self.part_2( |
| 73 | + self.get_input_lines(part=2) |
| 74 | + ) |
77 | 75 |
|
78 | 76 | def run(self): |
79 | | - solution1 = self.part_1() |
| 77 | + solution1 = self.part_1(self.get_input_lines(part=1)) |
80 | 78 | print(f"Day {self.day} - Part 1: {solution1}") |
81 | | - solution2 = self.part_2() |
| 79 | + solution2 = self.part_2(self.get_input_lines(part=2)) |
82 | 80 | print(f"Day {self.day} - Part 2: {solution2}\n") |
83 | 81 | return solution1, solution2 |
0 commit comments