diff options
| author | 2022-12-16 11:48:40 +0100 | |
|---|---|---|
| committer | 2022-12-16 11:48:58 +0100 | |
| commit | b37323ac00baa3ac5dcc042eaad95d4dc1eeab0f (patch) | |
| tree | c9418cdf2bda2f0689e1919c6c4d9cc230675ee1 /aoc | |
| parent | fa75898ed35c732ba4e9d455f16dee48ba6c5ca8 (diff) | |
| download | 2022-b37323ac00baa3ac5dcc042eaad95d4dc1eeab0f.tar.gz 2022-b37323ac00baa3ac5dcc042eaad95d4dc1eeab0f.tar.bz2 2022-b37323ac00baa3ac5dcc042eaad95d4dc1eeab0f.zip | |
Day 15 [WIP]
Diffstat (limited to 'aoc')
| -rw-r--r-- | aoc/__init__.py | 18 | ||||
| -rw-r--r-- | aoc/test_init.py | 4 |
2 files changed, 14 insertions, 8 deletions
diff --git a/aoc/__init__.py b/aoc/__init__.py index 8368f7b..7a089f6 100644 --- a/aoc/__init__.py +++ b/aoc/__init__.py | |||
| @@ -1,11 +1,15 @@ | |||
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | import os | 2 | import os |
| 3 | from abc import ABC | 3 | from abc import ABC |
| 4 | from typing import Generator, Any, Iterator | 4 | from typing import Generator, Any, Iterator, Dict, TypeVar, Generic |
| 5 | 5 | ||
| 6 | T = TypeVar("T") | ||
| 7 | I = TypeVar("I") | ||
| 6 | 8 | ||
| 7 | class BaseAssignment(ABC): | 9 | |
| 8 | example_result = NotImplemented | 10 | class BaseAssignment(Generic[T, I], ABC): |
| 11 | example_result: T = NotImplemented | ||
| 12 | example_kwargs: Dict = {} | ||
| 9 | 13 | ||
| 10 | def __init__(self, path): | 14 | def __init__(self, path): |
| 11 | self.path = path | 15 | self.path = path |
| @@ -13,14 +17,14 @@ class BaseAssignment(ABC): | |||
| 13 | def __str__(self): | 17 | def __str__(self): |
| 14 | return f"{self.__module__}.{self.__class__.__name__}" | 18 | return f"{self.__module__}.{self.__class__.__name__}" |
| 15 | 19 | ||
| 16 | def parse_item(self, item: str) -> Any: | 20 | def parse_item(self, item: str) -> I: |
| 17 | return item | 21 | return item |
| 18 | 22 | ||
| 19 | @property | 23 | @property |
| 20 | def part(self): | 24 | def part(self) -> int: |
| 21 | return 1 if self.__class__.__name__.endswith("One") else 2 | 25 | return 1 if self.__class__.__name__.endswith("One") else 2 |
| 22 | 26 | ||
| 23 | def read_input(self, example=False) -> Generator: | 27 | def read_input(self, example=False) -> Iterator[I]: |
| 24 | file = f"{self.path}/input.txt" | 28 | file = f"{self.path}/input.txt" |
| 25 | 29 | ||
| 26 | if example or not os.path.isfile(file): | 30 | if example or not os.path.isfile(file): |
| @@ -35,5 +39,5 @@ class BaseAssignment(ABC): | |||
| 35 | for line in input_file.readlines(): | 39 | for line in input_file.readlines(): |
| 36 | yield self.parse_item(line.strip("\n")) | 40 | yield self.parse_item(line.strip("\n")) |
| 37 | 41 | ||
| 38 | def run(self, input: Iterator) -> Any: | 42 | def run(self, input: Iterator[I]) -> T: |
| 39 | raise NotImplementedError("Please implement run") | 43 | raise NotImplementedError("Please implement run") |
diff --git a/aoc/test_init.py b/aoc/test_init.py index f804c41..e52daf7 100644 --- a/aoc/test_init.py +++ b/aoc/test_init.py | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | def test_assignment_examples(assignment): | 2 | def test_assignment_examples(assignment): |
| 3 | assert ( | 3 | assert ( |
| 4 | assignment.run(input=assignment.read_input(example=True)) | 4 | assignment.run( |
| 5 | input=assignment.read_input(example=True), **assignment.example_kwargs | ||
| 6 | ) | ||
| 5 | == assignment.example_result | 7 | == assignment.example_result |
| 6 | ) | 8 | ) |
