Refactor main entrypoint for UI.

This commit is contained in:
Mikael CAPELLE 2024-12-04 17:13:02 +01:00
parent 664dcfe7ba
commit 17432f7ac6

View File

@ -1,7 +1,9 @@
import argparse import argparse
import importlib import importlib
import json
import logging import logging
import sys import sys
from datetime import datetime
from pathlib import Path from pathlib import Path
from .base import BaseSolver from .base import BaseSolver
@ -11,6 +13,7 @@ def main():
parser = argparse.ArgumentParser("Holt59 Advent-Of-Code Runner") parser = argparse.ArgumentParser("Holt59 Advent-Of-Code Runner")
parser.add_argument("-v", "--verbose", action="store_true", help="verbose mode") 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("-t", "--test", action="store_true", help="test mode")
parser.add_argument("-a", "--api", action="store_true", help="API mode")
parser.add_argument( parser.add_argument(
"-u", "--user", type=str, default="holt59", help="user input to use" "-u", "--user", type=str, default="holt59", help="user input to use"
) )
@ -33,6 +36,7 @@ def main():
args = parser.parse_args() args = parser.parse_args()
verbose: bool = args.verbose verbose: bool = args.verbose
api: bool = args.api
test: bool = args.test test: bool = args.test
stdin: bool = args.stdin stdin: bool = args.stdin
user: str = args.user user: str = args.user
@ -54,7 +58,9 @@ def main():
f".{year}.day{day}", __package__ f".{year}.day{day}", __package__
).Solver ).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 data: str
if stdin: if stdin:
@ -63,5 +69,25 @@ def main():
with open(input_path) as fp: with open(input_path) as fp:
data = fp.read() data = fp.read()
start = datetime.now()
last = start
for i_answer, answer in enumerate(solver.solve(data.strip())): 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