From 4985ee94450df0bbcf982b7652c946a47707e60c Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Fri, 11 Dec 2020 23:53:45 +0100 Subject: Added AoC day 1 and 2 --- aoc/__init__.py | 24 ++++++++++++++++++++++++ aoc/__main__.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 aoc/__init__.py create mode 100644 aoc/__main__.py (limited to 'aoc') 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 @@ +import os +from abc import ABC +from collections import Iterator +from typing import Generator, Any + + +class AssignmentBase(ABC): + def __init__(self, path): + self.path = path + + def parse_item(self, item: str) -> Any: + return item + + def read_input(self, example = False) -> Generator: + file = f'{self.path}/input.txt' + if example or not os.path.isfile(file): + file = f'{self.path}/example.txt' + + with open(file, 'r') as input_file: + for line in input_file.readlines(): + yield self.parse_item(line.strip()) + + def run(self, input: Iterator) -> Any: + 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 @@ +import argparse +import os +import sys +import importlib + +def day(assignment_part: str) -> str: + return { + '1': 'One', + '2': 'Two', + }[assignment_part] + +parser = argparse.ArgumentParser(description='Advent of Code') + +parser.add_argument('day', type=str, help='Assignment day') +parser.add_argument( + '-p', '--part', type=day, nargs='?', default='1', + help='Assingment part. Defaults to one.' +) +parser.add_argument('--example', default=False, action='store_true') + + +if __name__ == '__main__': + args = parser.parse_args() + assignment_day = importlib.import_module(args.day) + + Assignment = getattr(assignment_day, f'Assignment{args.part}') + assignment = Assignment(path=os.path.dirname(assignment_day.__file__)) + + print(assignment.run(input=assignment.read_input())) -- cgit v1.2.3