diff --git a/src/holt59/aoc/2024/day11.py b/src/holt59/aoc/2024/day11.py index fea35bf..2f541f2 100644 --- a/src/holt59/aoc/2024/day11.py +++ b/src/holt59/aoc/2024/day11.py @@ -4,6 +4,13 @@ from typing import Any, Iterator from ..base import BaseSolver +def n_digits(n: int) -> int: + c = int(n == 0) + while n > 0: + c, n = c + 1, n // 10 + return c + + @cache def blink_one_stone(stone: int, round: int) -> int: if round == 0: @@ -12,10 +19,10 @@ def blink_one_stone(stone: int, round: int) -> int: if stone == 0: return blink_one_stone(1, round - 1) - if len((stone_s := str(stone))) % 2 == 0: - n = len(stone_s) - return blink_one_stone(int(stone_s[: n // 2]), round - 1) + blink_one_stone( - int(stone_s[n // 2 :]), round - 1 + if (n := n_digits(stone)) % 2 == 0: + p = 10 ** (n // 2) + return blink_one_stone(stone // p, round - 1) + blink_one_stone( + stone % p, round - 1 ) return blink_one_stone(stone * 2024, round - 1)