-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19-1.py
33 lines (27 loc) · 880 Bytes
/
day19-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
rules = {}
from itertools import product
with open("day19-1.txt") as f:
r, inp = f.read().split("\n\n")
for line in r.split("\n"):
ind, rule = line.strip().split(":")
if "\"" in rule:
rules[ind] = rule.strip().replace("\"", "")
else:
rules[ind] = [[x for x in rulez.strip().split(" ")] for rulez in rule.split("|")]
# construct possible cases
pos = '0'
cases = set()
def construct(pos):
if type(r := rules[pos]) == str:
return [r]
elif len(r) == 2:
return ["".join(x) for x in product(*[construct(x) for x in r[0]])] + ["".join(x) for x in product(*[construct(x) for x in r[1]])]
else:
return ["".join(x) for x in product(*[construct(x) for x in r[0]])]
cases = set(construct("0"))
# print(cases)
counter = 0
for l in inp.split("\n"):
if l.strip() in cases:
counter += 1
print(counter)