From 17432f7ac635bc5deb85e80f2094a77f2b09f580 Mon Sep 17 00:00:00 2001 From: Mikael CAPELLE Date: Wed, 4 Dec 2024 17:13:02 +0100 Subject: [PATCH] Refactor main entrypoint for UI. --- src/holt59/aoc/__main__.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/holt59/aoc/__main__.py b/src/holt59/aoc/__main__.py index a052fb8..7fba62d 100644 --- a/src/holt59/aoc/__main__.py +++ b/src/holt59/aoc/__main__.py @@ -1,7 +1,9 @@ import argparse import importlib +import json import logging import sys +from datetime import datetime from pathlib import Path from .base import BaseSolver @@ -11,6 +13,7 @@ def main(): parser = argparse.ArgumentParser("Holt59 Advent-Of-Code Runner") parser.add_argument("-v", "--verbose", action="store_true", help="verbose mode") parser.add_argument("-t", "--test", action="store_true", help="test mode") + parser.add_argument("-a", "--api", action="store_true", help="API mode") parser.add_argument( "-u", "--user", type=str, default="holt59", help="user input to use" ) @@ -33,6 +36,7 @@ def main(): args = parser.parse_args() verbose: bool = args.verbose + api: bool = args.api test: bool = args.test stdin: bool = args.stdin user: str = args.user @@ -54,7 +58,9 @@ def main(): f".{year}.day{day}", __package__ ).Solver - solver = solver_class(logging.getLogger("AOC"), verbose, year, day) + solver = solver_class( + logging.getLogger("AOC"), verbose=verbose, year=year, day=day, outputs=not api + ) data: str if stdin: @@ -63,5 +69,25 @@ def main(): with open(input_path) as fp: data = fp.read() + start = datetime.now() + last = start for i_answer, answer in enumerate(solver.solve(data.strip())): - print(f"answer {i_answer + 1} is {answer}") + current = datetime.now() + + if api: + print( + json.dumps( + { + "answer": i_answer + 1, + "value": answer, + "answerTime_s": (current - last).total_seconds(), + "totalTime_s": (current - start).total_seconds(), + } + ) + ) + else: + print( + f"answer {i_answer + 1} is {answer} (found in {(current - last).total_seconds():.2f}s)" + ) + + last = current