Skip to content

Commit abfb726

Browse files
feat: add memoization decorator
1 parent 13c6e29 commit abfb726

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/adventofcode/util/helpers.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import pstats
44
import time
5-
from typing import Callable, Literal
5+
from typing import Callable, Literal, Dict, Any
66

77
from adventofcode.config import RUNNING_ALL
88
from adventofcode.util.console import console
@@ -93,3 +93,17 @@ def wrapper(*args, **kwargs):
9393
return wrapper
9494

9595
return decorator
96+
97+
98+
def memoize(func: Callable): # type: ignore
99+
cache: Dict[Any, Any] = dict()
100+
101+
def memoized_func(*args):
102+
if args in cache:
103+
return cache[args]
104+
105+
result = func(*args)
106+
cache[args] = result
107+
return result
108+
109+
return memoized_func

0 commit comments

Comments
 (0)