diff options
Diffstat (limited to 'aoc')
| -rw-r--r-- | aoc/__init__.py | 14 | ||||
| -rw-r--r-- | aoc/__main__.py | 32 | ||||
| -rw-r--r-- | aoc/test_init.py | 6 | ||||
| -rw-r--r-- | aoc/utils.py | 3 |
4 files changed, 34 insertions, 21 deletions
diff --git a/aoc/__init__.py b/aoc/__init__.py index b13489c..5fce415 100644 --- a/aoc/__init__.py +++ b/aoc/__init__.py | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 1 | import os | 2 | import os |
| 2 | from abc import ABC | 3 | from abc import ABC |
| 3 | from typing import Generator, Any, Iterator | 4 | from typing import Generator, Any, Iterator |
| @@ -5,23 +6,24 @@ from typing import Generator, Any, Iterator | |||
| 5 | 6 | ||
| 6 | class BaseAssignment(ABC): | 7 | class BaseAssignment(ABC): |
| 7 | example_result = NotImplemented | 8 | example_result = NotImplemented |
| 9 | |||
| 8 | def __init__(self, path): | 10 | def __init__(self, path): |
| 9 | self.path = path | 11 | self.path = path |
| 10 | 12 | ||
| 11 | def __str__(self): | 13 | def __str__(self): |
| 12 | return f'{self.__module__}.{self.__class__.__name__}' | 14 | return f"{self.__module__}.{self.__class__.__name__}" |
| 13 | 15 | ||
| 14 | def parse_item(self, item: str) -> Any: | 16 | def parse_item(self, item: str) -> Any: |
| 15 | return item | 17 | return item |
| 16 | 18 | ||
| 17 | def read_input(self, example = False) -> Generator: | 19 | def read_input(self, example=False) -> Generator: |
| 18 | file = f'{self.path}/input.txt' | 20 | file = f"{self.path}/input.txt" |
| 19 | if example or not os.path.isfile(file): | 21 | if example or not os.path.isfile(file): |
| 20 | file = f'{self.path}/example.txt' | 22 | file = f"{self.path}/example.txt" |
| 21 | 23 | ||
| 22 | with open(file, 'r') as input_file: | 24 | with open(file, "r") as input_file: |
| 23 | for line in input_file.readlines(): | 25 | for line in input_file.readlines(): |
| 24 | yield self.parse_item(line.strip()) | 26 | yield self.parse_item(line.strip()) |
| 25 | 27 | ||
| 26 | def run(self, input: Iterator) -> Any: | 28 | def run(self, input: Iterator) -> Any: |
| 27 | raise NotImplementedError('Please implement run') \ No newline at end of file | 29 | raise NotImplementedError("Please implement run") |
diff --git a/aoc/__main__.py b/aoc/__main__.py index 25a7026..337bdfe 100644 --- a/aoc/__main__.py +++ b/aoc/__main__.py | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 1 | import argparse | 2 | import argparse |
| 2 | import importlib | 3 | import importlib |
| 3 | import os | 4 | import os |
| @@ -7,32 +8,37 @@ from typing import List | |||
| 7 | 8 | ||
| 8 | def day(assignment_part: str) -> str: | 9 | def day(assignment_part: str) -> str: |
| 9 | return { | 10 | return { |
| 10 | '1': 'One', | 11 | "1": "One", |
| 11 | '2': 'Two', | 12 | "2": "Two", |
| 12 | }[assignment_part] | 13 | }[assignment_part] |
| 13 | 14 | ||
| 15 | |||
| 14 | def kwargs(kwarg: str) -> List: | 16 | def kwargs(kwarg: str) -> List: |
| 15 | return kwarg.split('=') | 17 | return kwarg.split("=") |
| 18 | |||
| 16 | 19 | ||
| 17 | parser = argparse.ArgumentParser(description='Advent of Code') | 20 | parser = argparse.ArgumentParser(description="Advent of Code") |
| 18 | 21 | ||
| 19 | parser.add_argument('day', type=str, help='Assignment day') | 22 | parser.add_argument("day", type=str, help="Assignment day") |
| 20 | parser.add_argument( | 23 | parser.add_argument( |
| 21 | '-p', '--part', type=day, nargs='?', default='1', | 24 | "-p", |
| 22 | help='Assingment part. Defaults to one.' | 25 | "--part", |
| 26 | type=day, | ||
| 27 | nargs="?", | ||
| 28 | default="1", | ||
| 29 | help="Assingment part. Defaults to one.", | ||
| 23 | ) | 30 | ) |
| 24 | parser.add_argument('--example', default=False, action='store_true') | 31 | parser.add_argument("--example", default=False, action="store_true") |
| 25 | parser.add_argument('kwargs', type=kwargs, nargs='*') | 32 | parser.add_argument("kwargs", type=kwargs, nargs="*") |
| 26 | 33 | ||
| 27 | 34 | ||
| 28 | if __name__ == '__main__': | 35 | if __name__ == "__main__": |
| 29 | args = parser.parse_args() | 36 | args = parser.parse_args() |
| 30 | assignment_day = importlib.import_module(args.day) | 37 | assignment_day = importlib.import_module(args.day) |
| 31 | 38 | ||
| 32 | Assignment = getattr(assignment_day, f'Assignment{args.part}') | 39 | Assignment = getattr(assignment_day, f"Assignment{args.part}") |
| 33 | assignment = Assignment( | 40 | assignment = Assignment( |
| 34 | path=os.path.dirname(assignment_day.__file__), | 41 | path=os.path.dirname(assignment_day.__file__), **dict(args.kwargs) |
| 35 | **dict(args.kwargs) | ||
| 36 | ) | 42 | ) |
| 37 | 43 | ||
| 38 | start = perf_counter() | 44 | start = perf_counter() |
diff --git a/aoc/test_init.py b/aoc/test_init.py index a9155cc..f804c41 100644 --- a/aoc/test_init.py +++ b/aoc/test_init.py | |||
| @@ -1,2 +1,6 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 1 | def test_assignment_examples(assignment): | 2 | def test_assignment_examples(assignment): |
| 2 | assert assignment.run(input=assignment.read_input(example=True)) == assignment.example_result | 3 | assert ( |
| 4 | assignment.run(input=assignment.read_input(example=True)) | ||
| 5 | == assignment.example_result | ||
| 6 | ) | ||
diff --git a/aoc/utils.py b/aoc/utils.py index 6d794f4..a5de050 100644 --- a/aoc/utils.py +++ b/aoc/utils.py | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 1 | from typing import Callable | 2 | from typing import Callable |
| 2 | 3 | ||
| 3 | 4 | ||
| 4 | def bold(item: str, condition: Callable[[], bool]): | 5 | def bold(item: str, condition: Callable[[], bool]): |
| 5 | return f'\033[36m{item}\033[0m' if condition() else item | 6 | return f"\033[36m{item}\033[0m" if condition() else item |
