-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc19-2.py
43 lines (37 loc) · 807 Bytes
/
aoc19-2.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
34
35
36
37
38
39
40
41
42
from itertools import *
from functools import *
import re
rls = [l for l in iter(input,'')]
ms = [*iter(input,'')]
rs = {}
for l in rls:
n, r = l.split(': ')
rs[n] = [a.split(' ') for a in r.split(' | ')]
def match(m,r):
if re.match(r'"."',r):
return m and m[0] == r[1] and m[1:]
else:
for a in rs[r]:
M = m
for b in a:
M = match(M,b)
if M == False: break
else:
return M
return False
c = 0
for m in ms:
n = 0
# wishing I'd updated to 3.8 right about now
while True:
M = match(m,'42')
if M:
m = M
n += 1
else:
break
while m and n > 1:
m = match(m,'31')
n -= 1
c += m == ''
print(c)