aboutsummaryrefslogtreecommitdiff
path: root/libaoc.py
diff options
context:
space:
mode:
Diffstat (limited to 'libaoc.py')
-rw-r--r--libaoc.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/libaoc.py b/libaoc.py
new file mode 100644
index 0000000..1316691
--- /dev/null
+++ b/libaoc.py
@@ -0,0 +1,26 @@
+from io import TextIOWrapper
+from typing import Any, Callable, Iterable, Iterator, Optional, TypeVar
+
+S = TypeVar("S")
+T = TypeVar("T")
+matrix = list[list[T]]
+
+
+def flatten(iter: Iterable[Any]) -> Iterator[Any]:
+ if hasattr(iter, "__iter__") and type(iter) != str:
+ for i in iter:
+ yield from flatten(i)
+ else:
+ yield iter
+
+
+def read_int_matrix(f: TextIOWrapper) -> matrix[int]:
+ return list(map(lambda l: [int(n) for n in l.strip()], f.readlines()))
+
+
+def map2d(
+ func: Callable[[Any], Any],
+ iter: Iterable[Iterable[Any]],
+ const: Optional[Callable[[T], S]] = lambda x: x,
+) -> map:
+ return map(lambda i: const(map(func, i)), iter)