aboutsummaryrefslogtreecommitdiff
path: root/libaoc.py
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-12-12 06:56:03 +0100
committerThomas Voss <thomasvoss@live.com> 2021-12-12 06:56:03 +0100
commitb4195baae3b36bd997a97d24fd5e0ab38dcd5595 (patch)
treeeca4ea75f16be761da19409b83a45ce88dd1b0c5 /libaoc.py
parentc93fc1b9a11c96a4554ec47029bf085597fdef60 (diff)
Add libaoc
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)