Skip to content

Commit 76c5b80

Browse files
committed
refactor: simplify answer coercion
Could be a bit nicer once wimglenn/advent-of-code-data#151 is addressed.
1 parent 47d67f7 commit 76c5b80

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

src/aoc/__main__.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import typer
1111
from urllib3.exceptions import MaxRetryError
1212

13-
from aoc.base import BaseSolution
13+
from aoc.base import BaseSolution, Output
1414

1515
app = typer.Typer(no_args_is_help=True)
1616

@@ -192,9 +192,9 @@ def run(
192192
ans1, ans2 = solve(puzzle)
193193

194194
if ans1 is not None:
195-
out_file_a.write_text(ans1)
195+
out_file_a.write_text(str(ans1))
196196
if ans2 is not None:
197-
out_file_b.write_text(ans2)
197+
out_file_b.write_text(str(ans2))
198198

199199
rich.print(f"Part a: {ans1}\nPart b: {ans2}")
200200

@@ -215,22 +215,12 @@ def submit(
215215
ans1, ans2 = solve(puzzle)
216216

217217
if ans1 is not None:
218-
puzzle.answer_a = ans1
218+
puzzle.answer_a = ans1 # pyright: ignore[reportAttributeAccessIssue]
219219
if ans2 is not None:
220-
puzzle.answer_b = ans2
220+
puzzle.answer_b = ans2 # pyright: ignore[reportAttributeAccessIssue]
221221

222222

223-
def coerce_solution(solution: str | int | None) -> None | str:
224-
match solution:
225-
case None:
226-
return None
227-
case str():
228-
return solution
229-
case int():
230-
return str(solution)
231-
232-
233-
def solve(puzzle: Puzzle) -> tuple[str | None, str | None]:
223+
def solve(puzzle: Puzzle) -> tuple[Output | None, Output | None]:
234224
mod = importlib.import_module(f"aoc.Y{puzzle.year}.D{puzzle.day:0>2}.main")
235225

236226
solution = cast(BaseSolution[Any, Any], mod.Solution())
@@ -240,7 +230,7 @@ def solve(puzzle: Puzzle) -> tuple[str | None, str | None]:
240230
part1 = solution.part1(transformed)
241231
part2 = solution.part2(transformed)
242232

243-
return (coerce_solution(part1), coerce_solution(part2))
233+
return (part1, part2)
244234

245235

246236
def main() -> None:

0 commit comments

Comments
 (0)