-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.coffee
203 lines (178 loc) · 3.82 KB
/
19.coffee
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
_log = require 'ololog'
input = '''
Al => ThF
Al => ThRnFAr
B => BCa
B => TiB
B => TiRnFAr
Ca => CaCa
Ca => PB
Ca => PRnFAr
Ca => SiRnFYFAr
Ca => SiRnMgAr
Ca => SiTh
F => CaF
F => PMg
F => SiAl
H => CRnAlAr
H => CRnFYFYFAr
H => CRnFYMgAr
H => CRnMgYFAr
H => HCa
H => NRnFYFAr
H => NRnMgAr
H => NTh
H => OB
H => ORnFAr
Mg => BF
Mg => TiMg
N => CRnFAr
N => HSi
O => CRnFYFAr
O => CRnMgAr
O => HP
O => NRnFAr
O => OTi
P => CaP
P => PTi
P => SiRnFAr
Si => CaSi
Th => ThCa
Ti => BP
Ti => TiTi
e => HF
e => NAl
e => OMg
'''.split '\n'
molecule = 'CRnCaSiRnBSiRnFArTiBPTiTiBFArPBCaSiThSiRnTiBPBPMgArCaSiRnTiMgArCaSiThCaSiRnFArRnSiRnFArTiTiBFArCaCaSiRnSiThCaCaSiRnMgArFYSiRnFYCaFArSiThCaSiThPBPTiMgArCaPRnSiAlArPBCaCaSiRnFYSiThCaRnFArArCaCaSiRnPBSiRnFArMgYCaCaCaCaSiThCaCaSiAlArCaCaSiRnPBSiAlArBCaCaCaCaSiThCaPBSiThPBPBCaSiRnFYFArSiThCaSiRnFArBCaCaSiRnFYFArSiThCaPBSiThCaSiRnPMgArRnFArPTiBCaPRnFArCaCaCaCaSiRnCaCaSiRnFYFArFArBCaSiThFArThSiThSiRnTiRnPMgArFArCaSiThCaPBCaSiRnBFArCaCaPRnCaCaPMgArSiRnFYFArCaSiThRnPBPMgAr'
parse = ( input )->
r_in = /(\w+) => (\w+)/
rules = {}
for l in input
res = r_in.exec l
rules[res[1]]?=[]
rules[res[1]].push res[2]
rules
split_mol = ( mol )->
mol.match /[A-Z][a-z]*|e/g
####################################################
get_stat = ( rules, mol )->
alph = e:id:0
next_id = 1
for a in split_mol mol
alph[a]?={}
alph[a].id ?= next_id++
alph[a].dest = no
alph[a].src = no
alph[a].req = yes
for k, v of rules
alph[k]?={}
alph[k].id ?= next_id++
alph[k].dest ?= no
alph[k].src = yes
for m in v
for a in split_mol m
alph[a]?={}
alph[a].id ?= next_id++
alph[a].dest = yes
alph[a].src ?= no
alph
translate = ( stat, rules, mol )->
map = {}
root = '0'.charCodeAt()
leaf = 'a'.charCodeAt()
mid = 'E'.charCodeAt()
for k, v of stat
map[k] = String.fromCharCode switch
when not v.dest
root++
when not v.src
leaf++
else
mid++
_log.darkGray map
nmol = (for a in split_mol mol
map[a]
).join ''
nr = {}
for k,v of rules
nr[map[k]] = (
for m in v
(map[a] for a in split_mol m).join ''
)
[nr,nmol]
############################################
mutate = ( rules, mol )->
res = {}
for at, i in mol
l = mol[...i]
r = mol[i+1..]
for at2 in rules[at] ? []
res[l+at2+r]=1
Object.keys res
visited = {}
dump = 0
bruteforce = ( rules, start, end )->
steps = Infinity
unless visited[start]
visited[start]=1
_log start unless dump++%1000
for next in mutate rules, start
if next is end
return 1
if next.length < end.length
can = bruteforce rules, next, end
if steps > can
steps = can
steps
unrule = ( rules )->
unrules = {}
for k, v of rules
for a in v
unrules[a[0]]?={}
unrules[a[0]][a] = k
unrules
unmutate = ( unr, mol )->
res = []
set = {}
for a, i in mol
if unr[a]?
l = mol[...i]
for k, b of unr[a] when k is mol[i...i+k.length]
r = mol[i+k.length..]
next = l+b+r
if not set[next]
res.push next
set[next]=1
res.sort (a,b)->a.length-b.length # this does the trick!!! shorter first.
visited = {}
unbrute = ( unr, start, end, limit = Infinity, depth = 0 )->
if visited[start]
if visited[start] > depth
visited[start] = depth
Infinity
else
visited[start] = depth
_log.darkGray depth, start unless dump++%100000
for next in unmutate unr, start
if next is end
_log.green '! found at step', depth+1, dump
steps = depth+1
break
if depth < limit
can = unbrute unr, next, end, limit, depth+1
if steps > can
steps = can
steps
try
rules = parse input
stat = get_stat rules, molecule
[rules,molecule] = translate stat, rules, molecule
out = mutate rules, molecule
_log out.length
# will not complete in a lifetime.
#_log bruteforce rules, '0', molecule
_log.darkGray unrules = unrule rules
_log unbrute unrules, molecule, '0'
catch e
_log.red e