Solve 2024 day 11
This commit is contained in:
parent
96f15d07fc
commit
022b95eb97
31
2024/d11.py
Normal file
31
2024/d11.py
Normal file
@ -0,0 +1,31 @@
|
||||
from lib import get_data
|
||||
from lib import ints
|
||||
|
||||
data = get_data(__file__)
|
||||
xs = ints(data)
|
||||
CACHE = {}
|
||||
|
||||
# I had maxsize too small at first :/
|
||||
# @lru_cache(maxsize=10**12)
|
||||
def apply(x, rest):
|
||||
if rest == 0:
|
||||
return 1
|
||||
|
||||
if (x, rest) in CACHE:
|
||||
return CACHE[(x, rest)]
|
||||
|
||||
if x == 0:
|
||||
r = apply(1, rest - 1)
|
||||
elif len(str(x)) % 2 == 0:
|
||||
sx = str(x)
|
||||
a = int(sx[: len(sx) // 2])
|
||||
b = int(sx[len(sx) // 2 :])
|
||||
r = apply(a, rest - 1) + apply(b, rest - 1)
|
||||
else:
|
||||
r = apply(x * 2024, rest - 1)
|
||||
CACHE[(x, rest)] = r
|
||||
return r
|
||||
|
||||
|
||||
print(sum([apply(x, 25) for x in xs]))
|
||||
print(sum([apply(x, 75) for x in xs]))
|
Loading…
Reference in New Issue
Block a user