From b5ccff0ded219502182371b9668acee586ef7e62 Mon Sep 17 00:00:00 2001 From: Noah Mollerstuen <35637345+NoahMollerstuen@users.noreply.github.com> Date: Tue, 5 Dec 2023 00:47:32 -0500 Subject: [PATCH] Save feedback and compare answers against it --- .gitignore | 1 + util.py | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index f24c3fa..764aa65 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea/ web_secrets.py +puzzle_inputs diff --git a/util.py b/util.py index 326be77..50d06a2 100644 --- a/util.py +++ b/util.py @@ -1,4 +1,5 @@ import inspect +import json import os import shutil import time @@ -20,7 +21,7 @@ class Helper: def __init__(self, day=None, year=None, test_input=None, test_mode=True): self.day = day if day is not None else (datetime.datetime.now() + datetime.timedelta(minutes=10)).day - self.year = year if day is not None else datetime.date.today().year + self.year = year if year is not None else datetime.date.today().year self.raw_input = test_input if test_input and test_mode else self.load_input() self.raw_input = self.raw_input.strip("\n") @@ -90,7 +91,23 @@ def submit(self, answer=None, part=None, confirm_answer=True): if part is None: part = 2 if os.path.exists(f"{solutions_dir}/part1.py") else 1 - if confirm_answer: + attempts_path = f"{solutions_dir}/part{part}_attempts.json" + if os.path.exists(attempts_path): + with open(attempts_path) as f: + attempts = json.load(f) + else: + attempts = [] + + violates_feedback = False + for attempt, feedback in attempts: + if (feedback == "low" and answer <= attempt) or (feedback == "high" and answer >= attempt): + print(f"Answer {answer} violates previous feedback: {attempt} is too {feedback}.") + violates_feedback = True + elif feedback != "correct" and answer == attempt: + print(f"Answer {answer} was already submitted and is incorrect.") + violates_feedback = True + + if confirm_answer or violates_feedback: print(f"Submit {answer}?") user_input = input('>') if user_input not in ['y', 'yes', 'Y']: @@ -111,19 +128,29 @@ def submit(self, answer=None, part=None, confirm_answer=True): if response.status_code != 504: break print("504 error, retrying...") - time.sleep(2) + time.sleep(5) parsed_response = BeautifulSoup(response.text, features="html.parser") text = parsed_response.main.get_text().strip("\n") print(text) - if "That's the right answer!" in text: - if not os.path.exists(solutions_dir): - os.makedirs(solutions_dir) + if not os.path.exists(solutions_dir): + os.makedirs(solutions_dir) + if "That's the right answer!" in text: source_file = inspect.stack()[-1].filename shutil.copy(source_file, f"{solutions_dir}/part{part}.py") + feedback = "correct" + + else: + feedback = "low" if "too low" in text else ("high" if "too high" in text else "") + + attempts.append({"value": answer, "feedback": feedback}) + + with open(attempts_path, "w") as f: + json.dump(attempts, f) + class Grid: DIR4 = ((0, 1), (1, 0), (0, -1), (-1, 0))