-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
278 lines (252 loc) · 7.43 KB
/
index.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
const { solutions, guesses } = require('./words.json');
const fs = require('fs');
// just going to assume hard mode from now on, the old version's in git
guesses.push(...solutions);
// console.log(solutions.length, guesses.length);
function letterFrequency(sols = solutions) {
const counts = {};
for (const word of sols) for (const letter of word) {
if (counts[letter]) ++counts[letter]
else counts[letter] = 1;
}
return Object.entries(counts)
.map(([letter, count]) => ({ letter, count }))
.sort((a, b) => b.count - a.count);
}
function inOut(word) {
let i = 0, o = 0;
const r = new RegExp(`[${word}]`);
for (const sol of solutions) {
if (r.test(sol)) ++i;
else ++o;
}
return { i, o };
}
const GREEN = Symbol('🟩');
const YELLOW = Symbol('🟨');
const BLACK = Symbol('⬛️');
function guess(word, solution) {
if (!guesses.includes(word)) throw new Error('invalid guess: ' + word);
const a = [BLACK,BLACK,BLACK,BLACK,BLACK];
const used = [];
for (let i = 0; i < solution.length; ++i)
if (word[i] == solution[i]) {
a[i] = GREEN;
used[i] = true;
}
for (let i = 0; i < word.length; ++i)
for (let j = 0; j < solution.length; ++j) {
if (a[i] == GREEN || used[j] || i == j) continue;
if (word[i] == solution[j]) {
a[i] = YELLOW;
used[j] = true;
}
}
return a;
}
function guessString(...a) { return guess(...a).map(x => x.description).join(''); }
// console.log(guessString('later', 'orate'))
// console.log(guessString('idles', 'adder'))
// console.log(guessString('fussy', 'siege'))
function group(word, sols = solutions) {
const groups = {};
for (const sol of sols) {
const r = guessString(word, sol);
if (groups[r]) groups[r].push(sol);
else groups[r] = [sol];
}
return groups;
}
function groupCounts(g) {
for (const [r, sols] of Object.entries(g))
console.log(r, sols.length > 1 ? sols.length : sols[0]);
}
//groupCounts(group('orate'));
function similarGroups(sols) {
const groups = {};
for (const word of sols)
for (let i = 0; i < word.length; ++i) {
let pattern = word.split('');
pattern[i] = '.';
pattern = pattern.join('');
if (groups[pattern]) continue;
const r = new RegExp(`^${pattern}$`, 'g');
const group = sols.filter(w => r.test(w));
groups[pattern] = {
group,
letters: group.map(x => x[i]).join('')
};
}
return groups;
}
function similarity(sols) {
const g = similarGroups(sols);
return Math.max(...Object.values(g).map(g => g.letters.length));
}
function bestGuess({sols, guesses, guessesSoFar, path}) {
// this seems to be the best word you can start with
if (guessesSoFar.length == 0) {
return { word: 'blahs', groups: group('blahs', sols) };
}
// ok first things first
// strip out any words that are obviously total disasters
// we do NOT want to end up at ⬛️IGHT
const priorSimilarity = similarity(sols);
const similarities = guesses.map(guess => {
const groups = group(guess, sols);
const s = Object.values(groups).map(group => ({
group, similarity: similarity(group)
}));
return {
guess,
originalGroups: groups,
groups: s,
similarity: Math.max(...s.map(s => s.similarity))
};
}).sort((a, b) => a.similarity - b.similarity);
write(`${path}/similarities`, {
priorSimilarity, similarities
});
if (similarities[0].similarity < priorSimilarity) {
return {
word: similarities[0].guess,
groups: similarities[0].originalGroups
};
}
// if (HARD_MODE) {
// if (guessesSoFar.length == 1
// && guessesSoFar[0].r == '⬛️⬛️⬛️⬛️⬛️')
// {
// return { word: 'prams', groups: group('prams', sols) };
// }
// if (guessesSoFar.length == 1
// && guessesSoFar[0].r == '⬛️🟨⬛️⬛️⬛️')
// {
// return { word: 'merde', groups: group('merde', sols) };
// }
// }
// might not always be right but should always be good enough
const commonLetters = letterFrequency(sols)
.filter(({ count }) => count < sols.length)
.map(({ letter }) => letter);
// console.log(commonLetters);
const worstGuess = sols.length > 10;
const candidates = [];
// if (HARD_MODE) {
candidates.push(...guesses);
// } else {
// if (worstGuess) commonLetters.reverse();
// findWords: for (let i = 0x3e00000n; ; --i) {
// const use = i.toString(2).split('').map(x => x == '1');
// if (use.reduce((a, n) => a + n, 0) != 5) continue;
// const letters = commonLetters.filter((_, i) => use[i]);
// // console.log('L', letters.join(''))
// nextWord: for (const w of guesses) {
// if (guessesSoFar.some(g => g.guess == w)) continue;
// for (const letter of letters)
// if (!w.includes(letter)) continue nextWord;
// candidates.push(w);
// if (candidates.length == 50) break findWords;
// }
// }
// }
const g = candidates.map(word => {
const groups = group(word, sols);
if (worstGuess) {
const g = worstGroup(groups);
let score = 10000
- g.n * 100
- similarity(g.group) * 10
+ Math.abs(sols.length / 4 - g.group.length);
for (const k in groups) {
if (count(k.split(''), '🟩') > 2)
if (groups[k].length > 4)
score -= 1000;
}
return { word, groups, score };
}
return {
word, groups,
score: Math.max(...Object.values(groups).map(g => g.length))
};
});
g.sort((a, b) => a.score - b.score);
// console.log(JSON.stringify(g, (k,v)=>k=='groups'?null:v, 2));
// console.log(JSON.stringify(g,null,2));
// write('first-guess', g);
return g[0];
}
function worstGroup(groups) {
const m = Object.entries(groups).map(([r, group]) => {
const k = r.split('');
return {
r,
group,
n: `${count(k, '⬛️')}${count(k, '🟨')}`
};
});
m.sort((a,b) => b.n - a.n);
return m[0];
}
function count(arr, c) {
return arr.reduce((a,n) => a+(n==c), 0);
}
// console.log(bestGuess(solutions, guesses, [], Infinity).guess); process.exit(0);
// console.log(solutions.length)
const start = {
guessesLeft: 6,
sols: solutions, // .slice(0, 1000),
guessesSoFar: [],
path: 'hard',
guesses: guesses
};
let wins = 0;
const startedAt = Date.now();
function process(position) {
write(`${position.path}/sols`, position.sols);
if (position.sols.length <= position.guessesLeft
|| position.guessesSoFar[position.guessesSoFar.length - 1]?.r == '🟩🟩🟩🟩🟩') {
position.result = '✅ SUCCESS';
// console.log(position);
write(`${position.path}/success`, position);
return position;
}
if (position.guessesLeft <= 1) {
position.result = '❌ FAILURE';
console.log(position);
write(`${position.path}/failure`, position);
return position;
}
const { word, groups } = bestGuess(
position
);
position.guess = word; // bestGuess(position.sols, position.guessesSoFar.map(g => g.guess));
position.next = groups; // group(position.guess, position.sols);
for (const [key, sols] of Object.entries(position.next)) {
const nextPath = `${position.path}/${position.guess}-${key}-${sols.length}`;
fs.mkdirSync(nextPath, 0777, () => {});
position.next[key] = process({
// parent: position,
guessesLeft: position.guessesLeft - 1,
sols,
// lastClue: key,
guessesSoFar: [
...position.guessesSoFar,
{ guess: position.guess, r: key }
],
path: nextPath,
guesses: position.guesses.filter(w =>
guessString(position.guess, w) == key &&
!position.guessesSoFar.some(g => g.guess == w)
)
});
}
write(`${position.path}/results`, position);
return position;
}
const game = process(start);
write('game', game);
console.log((Date.now() - startedAt) / 1000);
function write(fn, pos) {
fs.writeFileSync(`${fn}.json`, JSON.stringify(pos));
}