summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2023-12-12 10:12:32 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2023-12-12 10:12:32 +0000
commit44877113c1b782f820464be7bc412751c63d7f1f (patch)
tree358017f225c839ff12b77ebd5a15074ffe962567
parent0b715584879ddea7d72a6b033faf8fa43595aa2e (diff)
downloadaoc2023-44877113c1b782f820464be7bc412751c63d7f1f.tar.gz
aoc2023-44877113c1b782f820464be7bc412751c63d7f1f.tar.xz
aoc2023-44877113c1b782f820464be7bc412751c63d7f1f.zip
day 12
-rw-r--r--12.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/12.py b/12.py
new file mode 100644
index 0000000..100c010
--- /dev/null
+++ b/12.py
@@ -0,0 +1,37 @@
+# pyright: strict
+import fnmatch
+from functools import cache
+from sys import stdin
+
+
+@cache
+def matching(
+ pattern: str, groups: tuple[int, ...], length: int, min_offset: int = 0
+) -> int:
+ if not groups:
+ return 1 if "#" not in pattern else 0
+ total = 0
+ for offset in range(min_offset, length - sum(groups) - len(groups) + 2):
+ base = "." * offset + "#" * groups[0]
+ if not fnmatch.fnmatch(base, pattern[: len(base)]):
+ continue
+ total += matching(
+ pattern[len(base) :], groups[1:], length - len(base), min_offset=1
+ )
+ return total
+
+
+inp: list[tuple[str, tuple[int, ...]]] = []
+for line in stdin:
+ pattern, rest = line.rstrip().split(maxsplit=1)
+ groups = tuple(map(int, rest.split(",")))
+ inp.append((pattern, groups))
+
+
+print(sum(matching(pattern, groups, len(pattern)) for pattern, groups in inp))
+
+p2 = 0
+for pattern, groups in inp:
+ pattern = "?".join([pattern] * 5)
+ p2 += matching(pattern, groups * 5, len(pattern))
+print(p2)