aoc2024/d11/run.py

25 lines
552 B
Python
Raw Permalink Normal View History

2024-12-11 10:57:36 +00:00
import sys
from math import log10
D = dict() # cache
2024-12-11 10:57:36 +00:00
def mutate(n:int, t:int) -> int:
x = D.get((n,t),-1)
if x < 0:
if t == 0:
x = 1
elif n == 0:
x = mutate(1, t-1)
elif (digits := int(log10(n))+1) & 1 == 0: # even nb of digits
l, r = divmod(n, 10**(digits//2))
x = mutate(l, t-1) + mutate(r, t-1)
else:
x = mutate(n*2024, t-1)
D[(n,t)] = x
return x
2024-12-11 10:57:36 +00:00
stones = [int(n) for n in sys.stdin.read().strip().split(" ")]
for times in [25,75]:
print(sum(mutate(stone, times) for stone in stones))