-
Notifications
You must be signed in to change notification settings - Fork 0
/
words_path.js
executable file
·52 lines (46 loc) · 1.08 KB
/
words_path.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
#!/usr/bin/env node
var from = process.argv[2]
var to = process.argv[3]
var words = new Set()
require('readline').createInterface({
input: require('fs').createReadStream('/usr/share/dict/words')
}).on('line', (line) => {
if (line.length == from.length) {
words.add(line.trim().toLowerCase())
}
}).on('close', () => {
console.log(bfs(from, to).join(' → '))
});
function siblings(from) {
var out = []
var str = from.split('');
for (var i = 0; i < str.length; i++) {
var prev = str[i]
for (var c = 'a'.charCodeAt(0); c <= 'z'.charCodeAt(0); c++) {
str[i] = String.fromCharCode(c)
w = str.join('')
if (words.delete(w)) {
out.push(w)
}
}
str[i] = prev
}
return out;
}
function bfs(from, to) {
var parent = new Map()
var queue = [from]
while (!parent.has(to) && queue.length > 0) {
var n = queue.shift()
for (var child of siblings(n)) {
parent.set(child, n)
queue.push(child)
}
}
var path = [to]
while (parent.has(to)) {
path.unshift(parent.get(to))
to = parent.get(to)
}
return path
}