-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomwalk.js
66 lines (56 loc) · 1.43 KB
/
randomwalk.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
let output
function print(s) {
output.append(s)
// TODO: scroll to bottom, but only if we were already scrolled to bottom.
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
function sortString(s) {
return s.split('').sort().join('')
}
function rand(n) {
return Math.floor(Math.random() * n)
}
function sample(a) {
return a[rand(a.length)]
}
function replace(s, i, c) {
return s.slice(0, i) + c + s.slice(i + 1);
}
let words
const anagrams = new Map()
const alphabet = 'abcdefghijklmnopqrstuvwxyz'
async function main() {
output = document.getElementById('output')
words = window.frames[0].document.body.textContent.split('\n')
words.forEach(word => {
const key = sortString(word)
if (!anagrams.has(key)) anagrams.set(key, [])
anagrams.get(key).push(word)
})
const keys = Array.from(anagrams.keys())
while (true) {
const key = sample(keys)
let word = sample(anagrams.get(key))
let last
for (let i = 0; i <= 999; i++) {
if (word !== last) {
print(word.toUpperCase() + ' ')
await sleep(100)
}
last = word
const letter = sample(alphabet)
const r = rand(5)
if (r < 3) {
word += letter
} else {
word = replace(word, rand(word.length), r < 4 ? '' : letter)
}
const next = anagrams.get(sortString(word))
word = next ? sample(next) : last
}
print('\n\n')
}
}
window.onload = main