Skip to content

Commit

Permalink
Merge pull request #160 from wimglenn/issue-133
Browse files Browse the repository at this point in the history
Always pre-check for an unlocked puzzle page when accessing examples.
  • Loading branch information
wimglenn authored Jan 4, 2025
2 parents 30bbea4 + 2ac2399 commit dfbe5fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
14 changes: 11 additions & 3 deletions aocd/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def _get_examples(self, parser_name="reference"):
# logs warning and returns an empty list if the parser plugin raises an
# exception for any reason.
try:
page = examples.Page.from_raw(html=self._get_prose())
page = examples.Page.from_raw(html=self._get_prose(force_precheck=True))
parser = _load_example_parser(name=parser_name)
if getattr(parser, "uses_real_datas", True):
datas = examples._get_unique_real_inputs(self.year, self.day)
Expand Down Expand Up @@ -737,9 +737,16 @@ def _request_puzzle_page(self):
_ensure_intermediate_dirs(self.prose0_path)
self.prose0_path.write_text(text, encoding="utf-8")

def _get_prose(self):
def _get_prose(self, force_precheck=False):
# prefer to return full prose (i.e. part b is solved or unlocked)
# prefer to return prose with answers from same the user id as self.user.id
if force_precheck:
unlocked_files = [
*AOCD_DATA_DIR.glob("*/" + self.prose1_path.name),
*AOCD_DATA_DIR.glob("*/" + self.prose2_path.name),
]
if not unlocked_files:
self._request_puzzle_page()
for path in self.prose2_path, self.prose1_path:
if path.is_file():
log.debug("_get_prose cache hit %s", path)
Expand All @@ -753,7 +760,8 @@ def _get_prose(self):
log.debug("_get_prose cache hit %s", self.prose0_path)
return self.prose0_path.read_text(encoding="utf-8")
log.debug("_get_prose cache miss year=%d day=%d", self.year, self.day)
self._request_puzzle_page()
if not force_precheck:
self._request_puzzle_page()
for path in self.prose2_path, self.prose1_path, self.prose0_path:
if path.is_file():
log.debug("_get_prose using %s", path)
Expand Down
7 changes: 4 additions & 3 deletions tests/test_example_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
<title>Day 1 - Advent of Code 1234</title>
<article>
<pre><code>test input data</code></pre>
<code>test answer_a</code>
<p>Your puzzle answer was <code>test answer_a</code>.</p>
</article>
<article>
<code>test answer_b</code>
<p>Your puzzle answer was <code>test answer_b</code>.</p>
</article>
<p>Both parts of this puzzle are complete! They provide two gold stars: **</p>
"""


Expand Down Expand Up @@ -70,7 +71,7 @@ def test_invalid_page_no_title():

def test_aoce(mocker, freezer, pook, capsys):
pook.get(
url="https://adventofcode.com:443/2022/day/1",
url="https://adventofcode.com/2022/day/1",
response_body=fake_prose,
)
freezer.move_to("2022-12-01 12:00:00-0500")
Expand Down
9 changes: 6 additions & 3 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,22 +374,25 @@ def test_user_from_unknown_id(aocd_config_dir):
User.from_id("blah")


def test_examples_cache(aocd_data_dir, pook):
def test_examples_cache(aocd_data_dir, pook, caplog):
caplog.set_level(logging.DEBUG)
mock = pook.get(
url="https://adventofcode.com/2014/day/1",
response_body=(
"<title>Day 1 - Advent of Code 2014</title>"
"<article><pre><code>1\n2\n3\n</code></pre><code>abc</code></article>"
"<article><pre><code>1\n2\n3\n</code></pre><code>xyz</code></article>"
"The first half of this puzzle is complete!"
"<p>Your puzzle answer was <code>answerA</code></p>"
),
times=1,
)
puzzle = Puzzle(day=1, year=2014)
assert mock.calls == 0
assert puzzle.examples[0].input_data == "1\n2\n3"
assert mock.calls == 1
assert puzzle.examples[0].input_data
assert mock.calls == 1
assert puzzle.examples[0].input_data == "1\n2\n3"
assert mock.calls == 1, "Should re-use cached result"


def test_example_partial(aocd_data_dir, pook):
Expand Down

0 comments on commit dfbe5fd

Please sign in to comment.