-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages1.py
72 lines (60 loc) · 1.88 KB
/
messages1.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
memo = {}
def matching(rule_num):
global memo
if rule_num not in memo:
rule = rules[rule_num]
if type(rule) == str:
res = {rule}
elif type(rule) == tuple:
if len(rule) == 1:
res = matching(rule[0])
else:
res = set()
r1, r2 = rule
for start in matching(r1):
for end in matching(r2):
res.add(start+end)
else:
r1, r2 = tuple(rule)
if len(r1) == 1:
res = matching(r1[0]) | matching(r2[0])
else:
res = set()
for start in matching(r1[0]):
for end in matching(r1[1]):
res.add(start+end)
temp = set()
for start in matching(r2[0]):
for end in matching(r2[1]):
temp.add(start+end)
res |= temp
memo[rule_num] = res
return memo[rule_num]
with open('/users/sysadmin/Documents/Prajwal/Programming/Competitions/Advent of Code/input.txt') as fin:
finished = False
rules = {}
while not finished:
line = fin.readline().strip()
if not line:
finished = True
else:
num, rule = line.split(': ')
num = int(num)
if '|' in rule:
rule = rule.split(' | ')
rule = set(map(lambda x: tuple(map(int, x.split())), rule))
elif '"' in rule:
rule = rule[1]
else:
rule = tuple(map(int, rule.split()))
rules[num] = rule
poss = matching(0)
res = 0
finished = False
while not finished:
word = fin.readline().strip()
if not word:
finished = True
else:
res += word in poss
print(res)