From 356fd35b08968eab3b1f30de36ea30222e3a9529 Mon Sep 17 00:00:00 2001 From: Mikael CAPELLE Date: Wed, 11 Dec 2024 09:57:47 +0100 Subject: [PATCH] 2024 day 11 without str. --- src/holt59/aoc/2024/day11.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) 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)