-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.js
More file actions
194 lines (164 loc) · 4.58 KB
/
Copy pathsolver.js
File metadata and controls
194 lines (164 loc) · 4.58 KB
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
const prompt = require('prompt-sync')({ sigint: true });
const fs = require('fs');
const frequencies = require('./frequencies');
const test = 0;
function scoreWord(word, letterFrequencies) {
let score = 0.0;
for (let i = 0; i < 26; i += 1) {
const char = String.fromCharCode('a'.charCodeAt(0) + i);
if (word.includes(char)) {
score += letterFrequencies.get(char);
}
}
return score;
}
function bestFromDict(dict, letterFrequencies) {
let best = '';
if (dict.length === 0) {
throw 'No words in dict';
}
let bestScore = 0;
dict.forEach((word) => {
const score = scoreWord(word, letterFrequencies);
if (score > bestScore) {
best = word;
bestScore = score;
}
});
return best;
}
function allIndices(word, char) {
let index = word.indexOf(char);
const indices = [];
while (index !== -1) {
indices.push(index);
index = word.indexOf(char, index + 1);
}
return indices;
}
function updateValid(dict, guess, colors) {
let next = dict;
const requiredFrequencies = new Map();
for (let i = 0; i < 5; i += 1) {
if (colors[i] === 'g') {
next = next.filter((word) => word[i] === guess[i]);
frequencies.incrementFrequency(requiredFrequencies, guess[i]);
} else if (colors[i] === 'y') {
next = next.filter((word) => word[i] !== guess[i] && word.indexOf(guess[i]) !== -1);
frequencies.incrementFrequency(requiredFrequencies, guess[i]);
}
}
for (let i = 0; i < 5; i += 1) {
if (colors[i] === 'b') {
const required = requiredFrequencies.has(guess[i]) ? requiredFrequencies.get(guess[i]) : 0;
next = next.filter((word) => {
return allIndices(word, guess[i]).length === required;
});
}
}
return next;
}
function diffStr(guess, answer) {
if (guess === answer) {
return 'ggggg';
}
let ret = ['b', 'b', 'b', 'b', 'b'];
for (let i = 0; i < 5; i += 1) {
if (guess[i] === answer[i]) {
ret[i] = 'g';
}
}
for (let i = 0; i < 5; i += 1) {
if (ret[i] === 'g') continue;
let index = answer.indexOf(guess[i]);
while (index !== -1 && ret[index] === 'g') {
index = answer.indexOf(guess[i], index + 1);
}
if (index !== -1) {
ret[i] = 'y';
}
}
return ret.join('');
}
function playGame(words) {
let win = false;
let dict = words;
for (let i = 0; i < 6 && !win; i += 1) {
const letterFrequencies = frequencies.calculateFrequencies(dict);
const best = bestFromDict(dict, letterFrequencies);
console.log(`Recommended Guess: ${best}`);
const guess = prompt('What was your guess? ');
const colors = prompt('What colors were shown? (Ex.) BYBBG: ').toLowerCase();
console.log('\n');
if (colors === 'ggggg') {
win = true;
console.log(`Yay! You win.\nRequired Guesses: ${i + 1}`);
} else {
dict = updateValid(dict, guess, colors);
}
}
if (!win) {
console.log('You lose :(\n (The bot is not yet perfect.)');
}
console.log('Play again tomorrow!');
}
function playAutomatic(dict, word) {
let i = 0;
let win = false;
let valid = dict;
for (i = 0; !win; i += 1) {
const letterFrequencies = frequencies.calculateFrequencies(valid);
const best = bestFromDict(valid, letterFrequencies);
const colors = diffStr(best, word);
if (colors === 'ggggg') {
win = true;
} else {
valid = updateValid(valid, best, colors);
}
}
return i;
}
function printStats(table) {
let min = Infinity;
let max = 0;
let total = 0;
table.forEach((value) => {
max = Math.max(max, value);
min = Math.min(min, value);
total += value;
});
const expectation = total / table.size;
let variance = 0;
table.forEach((value) => {
variance += Math.pow(value - expectation, 2);
});
variance /= table.size;
console.log(`Printing stats\nAverage: ${expectation}\nVariance: ${variance}\nMax: ${max}\nMin: ${min}`);
}
function testWords(solutions, permitted) {
const table = new Map();
solutions.forEach((word) => {
if (word === 'ultra') {
console.log('hi');
}
const requiredAttemps = playAutomatic(permitted, word);
table.set(word, requiredAttemps);
});
printStats(table);
}
if (fs.existsSync('words.txt')) {
fs.readFile('words.txt', 'ascii', (err1, data1) => {
const solutions = data1.split('\n');
if (test) {
fs.readFile('words.txt', 'ascii', (err2, data2) => {
const permitted = [...data2.split('\n'), ...solutions].sort();
testWords(solutions, solutions);
})
} else {
playGame(solutions);
}
});
} else {
console.error('Couldn\'t find words.txt.\n');
process.exit(1);
}