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