2024 day 11 without str.

This commit is contained in:
Mikael CAPELLE 2024-12-11 09:57:47 +01:00
parent 92bd85e1dd
commit 356fd35b08

View File

@ -4,6 +4,13 @@ from typing import Any, Iterator
from ..base import BaseSolver 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 @cache
def blink_one_stone(stone: int, round: int) -> int: def blink_one_stone(stone: int, round: int) -> int:
if round == 0: if round == 0:
@ -12,10 +19,10 @@ def blink_one_stone(stone: int, round: int) -> int:
if stone == 0: if stone == 0:
return blink_one_stone(1, round - 1) return blink_one_stone(1, round - 1)
if len((stone_s := str(stone))) % 2 == 0: if (n := n_digits(stone)) % 2 == 0:
n = len(stone_s) p = 10 ** (n // 2)
return blink_one_stone(int(stone_s[: n // 2]), round - 1) + blink_one_stone( return blink_one_stone(stone // p, round - 1) + blink_one_stone(
int(stone_s[n // 2 :]), round - 1 stone % p, round - 1
) )
return blink_one_stone(stone * 2024, round - 1) return blink_one_stone(stone * 2024, round - 1)