-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_19_small.py
82 lines (66 loc) · 1.24 KB
/
puzzle_19_small.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
73
74
75
76
77
78
79
80
81
82
import sys
import re
import copy
from collections import deque
ruleflag = 1
mesg = []
nterms = {}
terms = {}
E = set()
total = 0
CE = set()
def replace_compress():
for item in E:
lst = item.split(' ')
for i in range(len(lst)):
lst[i] = terms[lst[i]]
CE.add("".join(lst))
def all_done():
for item in E:
lst = item.split(' ')
if not all(trm in terms for trm in lst):
return item
return 0
def generate():
global E
item = all_done()
while item:
E.remove(item)
print(" ingenerate",len(E))
lst = list(item.split(' '))
for i in range(len(lst)):
if lst[i] in terms:
continue
for poss in nterms[lst[i]]:
E.add((' '.join(lst[0:i])+' ' +poss+' ' + ' '.join(lst[i+1:])).strip())
item = all_done()
for line in sys.stdin:
print(line)
line = line.strip()
line = re.sub('"','',line)
if line == '':
ruleflag = 0
continue
if ruleflag :
(lhs,rhs) = line.split(': ')
if rhs in 'ab':
terms[lhs] = rhs
else:
nterms[lhs] = []
rules = rhs.split(' | ')
for rl in rules:
nterms[lhs].append(rl)
else:
mesg.append(line)
print(nterms,"===")
print(terms)
print(mesg)
E.add('0')
generate()
replace_compress()
print(CE)
for msg in mesg:
print(msg,"ryinga")
if msg in CE:
total +=1
print(total)