From fd6b58022ddd8c54575b129ac14d74ff405bc9c1 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 21 Nov 2024 17:34:32 +0100 Subject: [PATCH] Review comments --- 2024/aoc/__main__.py | 13 ++++++++----- 2024/aoc/days/__init__.py | 6 +++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/2024/aoc/__main__.py b/2024/aoc/__main__.py index 2fb500d..c968a50 100644 --- a/2024/aoc/__main__.py +++ b/2024/aoc/__main__.py @@ -1,4 +1,5 @@ import datetime +import time import click @@ -14,17 +15,19 @@ from . import days default="-", help="Problem input file", ) -@click.option("-t", "--time", is_flag=True, help="Print elapsed time afterwards") +@click.option("-t", "--time", "timing", is_flag=True, help="Print elapsed time afterwards") @click.argument("day", required=True) -def main(day: int, time: bool, data: str) -> None: +def main(day: int, timing: bool, data: str) -> None: runner_class = days.get_runner(day) - start = datetime.datetime.now() + start = time.perf_counter_ns() part1, part2 = runner_class.run_both(data) - if time: - click.echo(f"Elapsed: {datetime.datetime.now() - start}", err=True) + if timing: + elapsed = time.perf_counter_ns() - start + delta = datetime.timedelta(microseconds=elapsed / 1000) + click.echo(f"Elapsed: {delta}", err=True) click.echo(part1) click.echo(part2) diff --git a/2024/aoc/days/__init__.py b/2024/aoc/days/__init__.py index cf5cd07..416ed5c 100644 --- a/2024/aoc/days/__init__.py +++ b/2024/aoc/days/__init__.py @@ -1,12 +1,12 @@ import importlib from abc import ABC, abstractmethod -from typing import Any, Tuple, cast +from typing import Any, cast class Runner(ABC): @classmethod @abstractmethod - def run_both(cls, data: str) -> Tuple[Any, Any]: + def run_both(cls, data: str) -> tuple[Any, Any]: pass @classmethod @@ -22,7 +22,7 @@ class Runner(ABC): class SeparateRunner(Runner): @classmethod - def run_both(cls, data: str) -> Tuple[Any, Any]: + def run_both(cls, data: str) -> tuple[Any, Any]: return (cls.part1(data), cls.part2(data))