diff options
Diffstat (limited to 'aoc')
| -rw-r--r-- | aoc/__init__.py | 24 | ||||
| -rw-r--r-- | aoc/__main__.py | 29 |
2 files changed, 53 insertions, 0 deletions
diff --git a/aoc/__init__.py b/aoc/__init__.py new file mode 100644 index 0000000..b07ead1 --- /dev/null +++ b/aoc/__init__.py | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | import os | ||
| 2 | from abc import ABC | ||
| 3 | from collections import Iterator | ||
| 4 | from typing import Generator, Any | ||
| 5 | |||
| 6 | |||
| 7 | class AssignmentBase(ABC): | ||
| 8 | def __init__(self, path): | ||
| 9 | self.path = path | ||
| 10 | |||
| 11 | def parse_item(self, item: str) -> Any: | ||
| 12 | return item | ||
| 13 | |||
| 14 | def read_input(self, example = False) -> Generator: | ||
| 15 | file = f'{self.path}/input.txt' | ||
| 16 | if example or not os.path.isfile(file): | ||
| 17 | file = f'{self.path}/example.txt' | ||
| 18 | |||
| 19 | with open(file, 'r') as input_file: | ||
| 20 | for line in input_file.readlines(): | ||
| 21 | yield self.parse_item(line.strip()) | ||
| 22 | |||
| 23 | def run(self, input: Iterator) -> Any: | ||
| 24 | raise NotImplementedError('Please implement run') | ||
diff --git a/aoc/__main__.py b/aoc/__main__.py new file mode 100644 index 0000000..da0496e --- /dev/null +++ b/aoc/__main__.py | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | import argparse | ||
| 2 | import os | ||
| 3 | import sys | ||
| 4 | import importlib | ||
| 5 | |||
| 6 | def day(assignment_part: str) -> str: | ||
| 7 | return { | ||
| 8 | '1': 'One', | ||
| 9 | '2': 'Two', | ||
| 10 | }[assignment_part] | ||
| 11 | |||
| 12 | parser = argparse.ArgumentParser(description='Advent of Code') | ||
| 13 | |||
| 14 | parser.add_argument('day', type=str, help='Assignment day') | ||
| 15 | parser.add_argument( | ||
| 16 | '-p', '--part', type=day, nargs='?', default='1', | ||
| 17 | help='Assingment part. Defaults to one.' | ||
| 18 | ) | ||
| 19 | parser.add_argument('--example', default=False, action='store_true') | ||
| 20 | |||
| 21 | |||
| 22 | if __name__ == '__main__': | ||
| 23 | args = parser.parse_args() | ||
| 24 | assignment_day = importlib.import_module(args.day) | ||
| 25 | |||
| 26 | Assignment = getattr(assignment_day, f'Assignment{args.part}') | ||
| 27 | assignment = Assignment(path=os.path.dirname(assignment_day.__file__)) | ||
| 28 | |||
| 29 | print(assignment.run(input=assignment.read_input())) | ||
