Skip to content

Commit 798716a

Browse files
committed
refacto(2024/Day11): Python using builtin cache
1 parent e8b2434 commit 798716a

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

2024/Day11/Day11.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from functools import cache
23

34
def algo(stone) :
45
if stone == 0 :
@@ -9,17 +10,12 @@ def algo(stone) :
910
else :
1011
return [2024*stone]
1112

12-
solvei_cache = {}
13+
@cache
1314
def solvei(stone, n) :
14-
if (stone, n) in solvei_cache :
15-
return solvei_cache[(stone, n)]
1615
res1 = algo(stone)
1716
if n == 1 :
18-
solvei_cache[(stone, n)] = len(res1)
1917
return len(res1)
20-
21-
solvei_cache[(stone, n)] = sum(map (lambda x : solvei(x, n-1), res1))
22-
return solvei_cache[(stone, n)]
18+
return sum(map (lambda x : solvei(x, n-1), res1))
2319

2420
def solve(arr, n) :
2521
return sum(map(lambda x : solvei(x, n), arr))

0 commit comments

Comments
 (0)