2024-12-11 09:57:47 +01:00

37 lines
860 B
Python

from functools import cache
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:
return 1
if stone == 0:
return blink_one_stone(1, 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)
class Solver(BaseSolver):
def solve(self, input: str) -> Iterator[Any]:
stones = list(map(int, input.split()))
yield sum(blink_one_stone(stone, 25) for stone in stones)
yield sum(blink_one_stone(stone, 75) for stone in stones)