Skip to content

Instantly share code, notes, and snippets.

@oerpli
Created December 20, 2021 06:19
Show Gist options
  • Save oerpli/32dddc9b557e79ba697ea15cdd918e4d to your computer and use it in GitHub Desktop.
Save oerpli/32dddc9b557e79ba697ea15cdd918e4d to your computer and use it in GitHub Desktop.
from collections import Counter, defaultdict
from pathlib import Path
from typing import List, Mapping, Tuple
from aocd.models import Puzzle
from dotenv import load_dotenv
load_dotenv() # API key in .env
class Solve:
def __init__(self, test_data, sample_sol, Y, D):
self.puzzle = Puzzle(year=Y, day=D)
self.solutionA = None
self.solutionB = None
self.test_data = test_data
self.testA, self.testB = sample_sol
self.passed_test = False
def parse_input(self, test: bool):
use = self.test_data if test else self.puzzle.input_data
return self.parser(use)
def parser(self, data):
return [*map(int, data.split(","))]
def solve_a(self, data):
raise NotImplementedError("Implement A")
def solve_b(self, data):
raise NotImplementedError("Implement B")
def format_stats(self):
stats = self.puzzle.my_stats
out = [f"{self.puzzle.day}"]
for p in "ab":
if x := stats.get(p):
t = x["time"]
ts = int(t.total_seconds())
out.append(f"{t} ({ts}s) {x['rank']} {x['score']}")
print("\t".join(out))
def submit(self, force=False, override_a=None, override_b=None):
if force or self.passed_test:
if self.testB is None and self.solutionA:
self.puzzle._submit(override_a or self.solutionA, part="a")
if self.testB and self.solutionB:
self.puzzle._submit(override_b or self.solutionB, part="b")
self.format_stats()
else:
print("Did not pass tests. Use force=True to submit nevertheless.")
def _complete(self, test: bool):
p = self.parse_input(test)
self.solutionA = self.solve_a(p)
try:
p = self.parse_input(test)
self.solutionB = self.solve_b(p)
except NotImplementedError as exc:
print("Part B todo")
print("== Test ==" if test else "== Real ==")
output = []
if test:
self.passed_test = True
if self.solutionA:
output.append("Part A")
if test:
a = self.testA == self.solutionA
self.passed_test &= a
output.extend(set([self.solutionA, self.testA]))
output.append(a)
else:
output.append(self.solutionA)
if self.testB is not None and self.solutionB:
output.append("\nPart B")
if test:
a = self.testB == self.solutionB
self.passed_test &= a
output.extend(set([self.solutionB, self.testB]))
output.append(a)
else:
output.append(self.solutionB)
print(" ".join(map(str, output)))
def test(self):
self._complete(True)
def full(self):
self._complete(False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment