61 lines
1.6 KiB
Python
61 lines
1.6 KiB
Python
import sys
|
|
from importlib import import_module
|
|
from inspect import getmembers, isclass
|
|
from time import time
|
|
from unittest import TextTestRunner, defaultTestLoader
|
|
|
|
from advent.functions import get_puzzle_input
|
|
|
|
from puzzles._solver import Solver
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) != 2:
|
|
raise RuntimeError("Usage: python solve.py <day>")
|
|
|
|
try:
|
|
day = int(sys.argv[1])
|
|
except ValueError:
|
|
raise RuntimeError("Day must be an integer")
|
|
|
|
print(f"Loading 2024, day {day} solver...")
|
|
try:
|
|
mod = import_module(f"puzzles.{day}")
|
|
except ModuleNotFoundError:
|
|
print(f"Solver for day {day} not found")
|
|
sys.exit(1)
|
|
|
|
solvers = getmembers(mod, lambda m: isclass(m) and issubclass(m, Solver))
|
|
if len(solvers) == 0:
|
|
raise RuntimeError("No solver found")
|
|
solver_class: type[Solver] = solvers[0][1]
|
|
|
|
test_suite = defaultTestLoader.loadTestsFromModule(mod)
|
|
test_runner = TextTestRunner(verbosity=0)
|
|
test_result = test_runner.run(test_suite)
|
|
if not test_result.wasSuccessful():
|
|
raise RuntimeError("Tests failed")
|
|
|
|
print("Fetching puzzle input...")
|
|
puzzle_input = get_puzzle_input(2024, int(day))
|
|
|
|
solver = solver_class(puzzle_input)
|
|
|
|
print("Solving part 1...")
|
|
start = time()
|
|
solution_p1 = solver.solve_p1()
|
|
print(f"Part 1 solution: {solution_p1} ({time() - start:.3f} seconds)")
|
|
|
|
print("Solving part 2...")
|
|
start = time()
|
|
solution_p2 = solver.solve_p2()
|
|
print(f"Part 2 solution: {solution_p2} ({time() - start:.3f} seconds)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as exc:
|
|
print(exc)
|
|
sys.exit(1)
|