Refactor code for API #3
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user