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