-
Notifications
You must be signed in to change notification settings - Fork 21
/
gestalt.js
52 lines (46 loc) · 1.09 KB
/
gestalt.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
var ngrams = new Array(256).fill(0);
var mask = 0;
var pressed = 0;
var guessed = 0;
function main(j) {
let fprob = 0;
let jprob = 0;
for (let i = 2; i < ngrams.length; i <<= 1) {
let fs = ngrams[mask % i | i];
let js = ngrams[mask % i | i | 1];
let total = js + fs;
if (total) {
// console.log(`${i}-grams: ${fs} f’s, ${js} j’s`);
fprob += fs / total;
jprob += js / total;
}
}
let prediction = jprob > fprob ? 'j' : 'f';
let correct = (jprob > fprob) == j;
guessed += correct;
console.log(`My prediction was \x1b[3${correct ? 2 : 1}m${prediction}\x1b[m`);
console.log(`Success rate so far: ${(100 * guessed / pressed).toFixed(1)}%`);
mask |= j;
for (let i = 2; i < ngrams.length; i <<= 1)
ngrams[mask % i | i] += Math.pow(1.05, pressed - guessed);
mask <<= 1;
mask %= ngrams.length;
}
var stdin = process.stdin;
if (stdin.setRawMode)
stdin.setRawMode(true);
stdin.on('data', (buffer) => {
for (let key of buffer) {
switch (key) {
case 102:
case 106:
++pressed;
main(key == 106);
break;
case 3:
case 4:
case 113:
process.exit();
}
}
});