summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2020-12-21 11:54:02 +0100
committerGustav Sörnäs <gustav@sornas.net>2020-12-21 11:54:02 +0100
commit0a90b3cead9b4e0a19c962f79a9170fc2781f791 (patch)
tree7dd958bd630e94a7df3d050fb901728823f6d595
parent2524a8856bb743139a107d59f14798f5e87231ce (diff)
downloadaoc-0a90b3cead9b4e0a19c962f79a9170fc2781f791.tar.gz
19.1
-rw-r--r--20/py/d19.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/20/py/d19.py b/20/py/d19.py
new file mode 100644
index 0000000..7317c44
--- /dev/null
+++ b/20/py/d19.py
@@ -0,0 +1,31 @@
+import aoc20
+import sys
+from collections import defaultdict
+import functools
+import re
+
+
+def pt1(_in):
+ rules = dict()
+ for rule in "".join(_in).split("\n\n")[0].split("\n"):
+ rule = rule.replace("\"", "").split(": ")
+ rules[rule[0]] = rule[1] if rule[1] in "ab" else "( {} )".format(rule[1])
+
+ @functools.cache
+ def format_rule(rule):
+ if rule not in rules:
+ return rule
+ return " ".join(format_rule(token) for token in rules[rule].split())
+
+ regex = re.compile(format_rule("0").replace(" ", ""))
+ return sum(1 for line in "".join(_in).split("\n\n")[1][:-1].split("\n") if regex.fullmatch(line))
+
+
+def pt2(_in):
+ pass
+
+
+if __name__ == "__main__":
+ _in = aoc20.read_input(sys.argv[1:], 19)
+ print(pt1(_in))
+ print(pt2(_in))