-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.js
376 lines (293 loc) · 8.83 KB
/
solver.js
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
Available functions:
init()
reinit(arg) - 'all', 'anagrams', 'words', 'wordDict'
findSolutionsByWord(wordToFind)
findSolutionsBySample(sampleSize)
sampleSolutions(amount)
filterSolutions(word)
*/
function createSolver() {
let solver = {}
let anagrams
let words
let wordDict
let solutions
const numOfBlocks = blocks.length
const defaultSampleSize = 100
const defaultRetrieveAmount = 100
solver.init = function() {
if (!anagrams) {
getAnagrams()
}
if (!words) {
getWords()
}
if (!wordDict) {
buildWordDict()
}
console.log('Done')
function getAnagrams() {
anagrams = {}
if (localStorage.getItem('anagrams')) {
console.log('Retrieving anagrams...')
anagrams = JSON.parse(localStorage.getItem('anagrams'))
} else {
console.log('Generating anagrams...')
let rawAnagrams = listCombinations(blocks, '')
rawAnagrams.forEach((anagram) => {
anagrams[canonicalVersion(anagram)] = 1
})
delete anagrams['']
localStorage.setItem('anagrams', JSON.stringify(anagrams))
}
}
function getWords() {
words = []
if (localStorage.getItem('words')) {
console.log('Retrieving words...')
words = JSON.parse(localStorage.getItem('words'))
} else {
console.log('Loading words...')
wordList = rawWords.trim().toLowerCase().split('\n')
nameList = names.trim().toLowerCase().split('\n')
words = _.uniq(wordList.concat(nameList))
console.log('Removing impossible words...')
words = words.filter((word) => {
return canWork(word)
})
console.log('Generating canonical reverse indices...')
words = words.map((word) => {
return {
original: word,
canonical: canonicalVersion(word)
}
})
console.log('Sorting by canonical index...')
words = words.sort((a, b) => {
if (a.canonical > b.canonical){
return 1
} else {
return -1
}
})
console.log('Combining identical indices...')
words = words.reduce((soFar, next, index, array) => {
if ((soFar.length == 0) || (array[index-1].canonical != next.canonical)) {
soFar.push({
canonical: next.canonical,
list: [next.original]
})
} else {
soFar[soFar.length-1].list.push(next.original)
}
return soFar
}, [])
localStorage.setItem('words', JSON.stringify(words))
}
}
function buildWordDict() {
wordDict = {}
console.log('Building word dictionary...')
words.forEach((word) => {
wordDict[word.canonical] = word.list
})
}
}
solver.reinit = function(arg) {
arg = arg || 'all'
if (arg == 'all') {
localStorage.removeItem('anagrams')
anagrams = undefined
localStorage.removeItem('words')
words = undefined
wordDict = undefined
} else if (arg == 'anagrams') {
localStorage.removeItem('anagrams')
anagrams = undefined
} else if (arg == 'words') {
localStorage.removeItem('words')
words = undefined
} else if (arg == 'wordDict') {
wordDict = undefined
} else {
console.log('Didn\'t understand that argument.')
return
}
solver.init()
}
// Find all the solutions that contain this word
solver.findSolutionsByWord = function(wordToFind) {
wordToFind = wordToFind || ''
wordToFind = wordToFind.toLowerCase()
console.log('Checking possibility...')
if (!canWork(wordToFind)) {
console.log('No solutions contain "' + wordToFind + '".')
return false
}
console.log('Creating initial solution set...')
let word = words.filter(word => {
return word.list.includes(wordToFind)
})
let firstSet = [{
canonical: word[0].canonical,
factors: [[word[0].canonical]]
}]
solutions = findSolutions(words, firstSet)
return true
}
// Find all the solutions by randomly restricting the available words to a certain number
solver.findSolutionsBySample = function(sampleSize) {
sampleSize = sampleSize || defaultSampleSize
console.log('Sampling word list...')
let wordList = _.sampleSize(words, sampleSize)
let firstSet = firstSolutions(wordList)
solutions = findSolutions(wordList, firstSet)
function firstSolutions(wordList) {
// Simply copy the words into the first solution list
let firstList = []
wordList.forEach((word) => {
let copy = {
canonical: word.canonical,
factors: [[word.canonical]]
}
firstList.push(copy)
})
return firstList
}
}
// Get a random sample of the solutions already found
solver.sampleSolutions = function(amount) {
amount = amount || defaultRetrieveAmount
return _.sampleSize(solutions, amount)
}
// Return only the solutions that contain a particular word
solver.filterSolutions = function(word) {
return solutions.filter(s => {
return s.includes(word)
})
}
solver.getSolutions = function() {
return solutions
}
return solver
/*
Main algorithm. Starts with one word solutions, which is exactly the words data
structure. Next it combines them all with each other, finding the two word
solutions. Then it combines these with the one word solutions, which finds
the three word solutions. And so on, until it cannot continue.
*/
function findSolutions(wordList, firstSet) {
console.log('Finding solutions...')
let result = []
let solutionSet = firstSet
let wordCount = 1
while (solutionSet.length > 0) {
result.push(solutionSet)
wordCount++
console.log('Finding solutions with ' + wordCount + ' words...')
solutionSet = nextSolutions(solutionSet)
}
console.log('No solutions with ' + wordCount + ' words.')
console.log('Combining all solutions...')
result = _.flatten(result)
console.log('Translating solutions...')
result = result.map(translateSolution)
result = _.flatten(result)
console.log('Done.')
return result
// By far, the most compute-intensive function
function nextSolutions(solutions) {
let nextSolutions = {}
// For every solution, try combining it with every word
// This double loop is the bottleneck of the entire algorithm
for (let sol = 0 ; sol < solutions.length ; sol++) {
let solCanon = solutions[sol].canonical
let maxLength = numOfBlocks - solCanon.length
for (let wrd = 0 ; wrd < wordList.length ; wrd ++) {
let wrdCanon = wordList[wrd].canonical
// Some optimization - a length check is faster than canWorkFast(canonicalVersion(solCanon + wrdCanon))
if (wrdCanon.length > maxLength) {
continue
}
let combined = canonicalVersion(solCanon + wrdCanon)
if (canWorkFast(combined)) {
addToSolutionSet(nextSolutions, combined, solutions[sol], wrdCanon)
}
}
}
// Convert back to list
let result = []
for (let solution in nextSolutions) {
result.push({
canonical: solution,
factors: nextSolutions[solution]
})
}
return result
}
// This isn't the bottleneck, but any optimization here would be pretty big, and there definitely could be optimization
function addToSolutionSet(solutionSet, canonical, prev, factor) {
let newFactors = prev.factors.map((factorList) => {
return factorList.concat(factor).sort()
})
if (solutionSet[canonical]) {
solutionSet[canonical] = _.uniqBy(solutionSet[canonical].concat(newFactors), (list) => {
return list.join('-')
})
} else {
solutionSet[canonical] = newFactors
}
}
function translateSolution(solution) {
// Replace each canonical form with the array of real words
let expanded = solution.factors.map((factors) => {
return factors.map((word) => {
return wordDict[word]
})
})
// Generate the possible sentences with the real words
let translated = expanded.map((list) => {
return listCombinations(list, ' ')
})
return _.flatten(translated)
}
}
/*
Only util functions beneath this line
*/
function canonicalVersion(str) {
let chars = str.split('')
let sorted = chars.sort()
return sorted.join('')
}
function canWork(str) {
return canWorkFast(canonicalVersion(str))
}
// Assumes str is in canonical version and anagrams has been built
function canWorkFast(str) {
if (anagrams[str] !== undefined) {
return true
} else {
return false
}
}
// Given an array of arrays of strings, get all combinations of contents, with one selection per child array
function listCombinations(array, separator) {
let numOfChoices = array.reduce((soFar, current) => {return soFar * current.length}, 1)
let choices = []
// Iterate through all choices
for (let choice = 0 ; choice < numOfChoices ; choice++) {
let row = []
// With the choice we have, select one element from each subarray
let choiceVar = choice
for (let subarray = 0 ; subarray < array.length ; subarray++) {
let index = choiceVar % array[subarray].length
row[subarray] = array[subarray][index]
choiceVar = Math.round((choiceVar - index) / array[subarray].length)
}
choices[choice] = row.join(separator)
}
return choices
}
}