Solve 2024 day 11
This commit is contained in:
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]))
|
||||||
@@ -301,4 +301,6 @@ and focus. Of course, learning more algorithms and techniques helps.
|
|||||||
- Day 8: `00:15:59 1742 0 00:26:56 2190 0`
|
- Day 8: `00:15:59 1742 0 00:26:56 2190 0`
|
||||||
- Day 9: `00:48:23 6055 0 01:09:38 3159 0`
|
- Day 9: `00:48:23 6055 0 01:09:38 3159 0`
|
||||||
- Day 10: `00:19:47 2976 0 00:20:47 2244 0`
|
- Day 10: `00:19:47 2976 0 00:20:47 2244 0`
|
||||||
|
- Day 11: `11 00:05:13 642 0 00:33:07 2673 0`
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user