day 11: explore recursively, use cache

This commit is contained in:
setop 2024-12-11 11:57:36 +01:00
parent 4aa762f073
commit 422e5440ff
1 changed files with 21 additions and 0 deletions

21
d11/run.py Normal file
View File

@ -0,0 +1,21 @@
import sys
from math import log10
from functools import cache
@cache
def mutate(n:int, t:int) -> int:
if t == 0:
return 1
if n == 0:
return mutate(1, t-1)
if (digits := int(log10(n))+1) & 1 == 0:
return count(divmod(n, 10**(digits//2)), t-1)
return mutate(n*2024, t-1)
def count(stones: list[int], times: int) -> int:
return sum(mutate(stone, times) for stone in stones)
stones = [int(n) for n in sys.stdin.read().strip().split(" ")]
print(count(stones, 25))
print(count(stones, 75))