diff options
| author | 2023-11-19 16:55:03 +0100 | |
|---|---|---|
| committer | 2023-11-19 16:55:03 +0100 | |
| commit | 4147da1317c19fa61d6aa265e8370e63231f9207 (patch) | |
| tree | 316925e780126c97fb10342e3175fd0e6b0aa3fb /aoc/__main__.py | |
| download | 2023-4147da1317c19fa61d6aa265e8370e63231f9207.tar.gz 2023-4147da1317c19fa61d6aa265e8370e63231f9207.tar.bz2 2023-4147da1317c19fa61d6aa265e8370e63231f9207.zip | |
Initial commit
Diffstat (limited to 'aoc/__main__.py')
| -rw-r--r-- | aoc/__main__.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/aoc/__main__.py b/aoc/__main__.py new file mode 100644 index 0000000..9bbaed9 --- /dev/null +++ b/aoc/__main__.py | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | import enum | ||
| 2 | import typing | ||
| 3 | import importlib | ||
| 4 | import os | ||
| 5 | from pathlib import Path | ||
| 6 | from shutil import copytree | ||
| 7 | from time import perf_counter | ||
| 8 | from typing import List, Callable | ||
| 9 | import typer | ||
| 10 | |||
| 11 | app = typer.Typer() | ||
| 12 | |||
| 13 | |||
| 14 | class AssignmentPart(str, enum.Enum): | ||
| 15 | One = "1" | ||
| 16 | Two = "2" | ||
| 17 | |||
| 18 | |||
| 19 | def day(assignment_part: str) -> str: | ||
| 20 | return assignment_part | ||
| 21 | |||
| 22 | |||
| 23 | def kwargs(kwarg: str) -> List: | ||
| 24 | return kwarg.split("=") | ||
| 25 | |||
| 26 | |||
| 27 | @app.command() | ||
| 28 | def run(day: str, | ||
| 29 | part: str = typer.Option("One", '--part', help="Assignment part. Defaults to 'One'.", show_choices=True), | ||
| 30 | example: bool = typer.Option(False, '--example', help="Use an example input file"), | ||
| 31 | kwargs: List[str] = typer.Argument(None) | ||
| 32 | ): | ||
| 33 | |||
| 34 | assignment_day = importlib.import_module(day) | ||
| 35 | |||
| 36 | Assignment = getattr(assignment_day, f"Assignment{part}") | ||
| 37 | assignment = Assignment( | ||
| 38 | path=os.path.dirname(assignment_day.__file__), **dict(kwargs) | ||
| 39 | ) | ||
| 40 | |||
| 41 | start = perf_counter() | ||
| 42 | typer.echo( | ||
| 43 | assignment.run( | ||
| 44 | input=assignment.read_input(example=example), | ||
| 45 | ) | ||
| 46 | ) | ||
| 47 | end = perf_counter() | ||
| 48 | delta = end - start | ||
| 49 | typer.secho(f'\n{(delta if delta > 1 else delta * 1000):.3f}{"s" if delta > 1 else "ms"}', fg="green") | ||
| 50 | |||
| 51 | |||
| 52 | @app.command() | ||
| 53 | def new(day: str): | ||
| 54 | self = importlib.import_module(".", package="aoc") | ||
| 55 | path = Path(self.__file__) | ||
| 56 | template = path.parent.joinpath("template") | ||
| 57 | target = path.parent.parent.joinpath(day) | ||
| 58 | |||
| 59 | try: | ||
| 60 | copytree(template, target) | ||
| 61 | except FileExistsError: | ||
| 62 | typer.secho(f"{day} already exists", fg="red") | ||
| 63 | |||
| 64 | |||
| 65 | if __name__ == "__main__": | ||
| 66 | app() | ||
