Skip to content

Commit

Permalink
expose puzzle at the top level (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mxbonn authored Nov 20, 2024
1 parent f4850c2 commit 790e599
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions aocd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"get_data",
"models",
"post",
"puzzle",
"runner",
"submit",
"types",
Expand All @@ -37,13 +38,17 @@

if t.TYPE_CHECKING:
data: str
puzzle: models.Puzzle
submit = _impartial_submit


def __getattr__(name: str) -> t.Any:
if name == "data":
day, year = get_day_and_year()
return get_data(day=day, year=year)
if name == "puzzle":
day, year = get_day_and_year()
return get.get_puzzle(day=day, year=year)
if name == "submit":
try:
day, year = get_day_and_year()
Expand Down
34 changes: 34 additions & 0 deletions aocd/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,40 @@ def get_data(
return puzzle.input_data


def get_puzzle(
session: str | None = None,
day: int | None = None,
year: int | None = None,
block: bool = False,
) -> Puzzle:
"""
Get puzzle for day (1-25) and year (2015+).
User's session cookie (str) is needed - puzzle inputs differ by user.
If `block` is True and the puzzle is still locked, will wait until unlock
before returning data.
"""
if session is None:
user = default_user()
else:
user = User(token=session)
if day is None:
day = current_day()
log.info("current day=%s", day)
if year is None:
year = most_recent_year()
log.info("most recent year=%s", year)
puzzle = Puzzle(year=year, day=day, user=user)
try:
_ = puzzle.input_data
return puzzle
except PuzzleLockedError:
if not block:
raise
q = block == "q"
blocker(quiet=q, until=(year, day))
return puzzle


def most_recent_year() -> int:
"""
This year, if it's December.
Expand Down

0 comments on commit 790e599

Please sign in to comment.