-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwordle.ts
More file actions
468 lines (404 loc) · 14.4 KB
/
wordle.ts
File metadata and controls
468 lines (404 loc) · 14.4 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Everything is strings of characters to avoid Unicode weirdness.
// To check for matches, we need to draft an algorithm for scoring.
//
// An initial easy check is "do we have all green letters correct". If any
// greens are incorrect, then the word doesn't match. Simple.
//
// The harder check is "do we have the right letters". For this one, we
// remember our scoring patterns:
// - "none" means "exactly 0"
// - "green" means "at least 1"
// - "yellow" means "at least 1"
// - "yellow none" means "exactly 1"
//
// To make this more algorithmic, we can remember these easy patterns:
// - if we see a yellow or green letter, we must have it somewhere
// - if we see a grey letter, it places a cap on how many letters we have
//
// One yellow means we have at least one of that letter, and one yellow and
// a none means we have exactly one. Simple.
export type ScoreMark = "partial" | "correct" | undefined
export class Score {
static of(correct: Word, guess: Word): Score {
const cc = correct.word.slice() as (string | undefined)[]
const ogc = guess.word.slice()
const gc = ogc.slice() as (string | undefined)[]
if (cc.length != gc.length) {
throw new Error("Both words must be the same length.")
}
const marks: ScoreMark[] = Array.from({ length: gc.length })
for (let i = 0; i < gc.length; i++) {
// This will never run `undefined == undefined` since the undefined values
// only appear after the `for` loop is run to completion.
if (gc[i] == cc[i]) {
marks[i] = "correct"
// Stops characters in the solution from being used twice
cc[i] = undefined
// Stops correctly guessed characters from being marked as partials
gc[i] = undefined
}
}
for (let i = 0; i < gc.length; i++) {
const guess = gc[i]
// Filter out values which we already marked as correct
if (guess == null) continue
const ccIndex = cc.indexOf(guess)
if (ccIndex == -1) continue
marks[i] = "partial"
// Stops characters in the solution from being used twice
cc[i] = undefined
}
return new Score(marks, guess)
}
readonly hits: { readonly [char: string]: number }
readonly misses: { readonly [char: string]: true }
constructor(
readonly marks: readonly ScoreMark[],
readonly guess: Word,
) {
if (marks.length != guess.word.length) {
throw new Error("`marks` and `guess` arrays must have same length.")
}
Object.freeze(marks)
Object.freeze(guess)
const hits: Record<string, number> = (this.hits = Object.create(null))
const misses: Record<string, true> = (this.misses = Object.create(null))
for (let i = 0; i < guess.word.length; i++) {
const mark = marks[i]!
const char = guess.word[i]!
// If grey, note that we missed it. This means we have an exact value.
if (mark == null) {
misses[char] = true
}
// Otherwise, note that we hit it.
else if (char in hits) {
hits[char]++
} else {
hits[char] = 1
}
}
Object.freeze(hits)
Object.freeze(misses)
Object.freeze(this)
}
}
export class Word {
static of(word: string): Word {
return new Word(Array.from(word))
}
readonly chars: { readonly [char: string]: number }
constructor(readonly word: readonly string[]) {
Object.freeze(word)
const chars: Record<string, number> = (this.chars = Object.create(null))
for (let i = 0; i < word.length; i++) {
const char = word[i]!
if (char in chars) {
chars[char]++
} else {
chars[char] = 1
}
}
Object.freeze(chars)
Object.freeze(this)
}
matches(score: Score): boolean {
for (let i = 0; i < score.guess.word.length; i++) {
if (
score.marks[i] == "correct"
&& this.word[i] !== score.guess.word[i]
) {
return false
}
}
const { hits, misses } = score
const { chars } = this
for (const key in hits) {
if ((chars[key] || 0) < hits[key]!) {
return false
}
}
for (const key in misses) {
if ((chars[key] || 0) != (hits[key] || 0)) {
return false
}
}
return true
}
equals(other: Word): boolean {
return (
this.word.length == other.word.length
&& this.word.every((a, i) => other.word[i] === a)
)
}
toString() {
return this.word.join("")
}
}
export class WordList {
constructor(readonly words: readonly Word[]) {}
includes(word: Word) {
return this.words.some((a) => a.equals(word))
}
matchesWord(guess: Word) {
return new WordList(
this.words.filter((word) => word.matches(Score.of(word, guess))),
)
}
matchesScore(score: Score) {
return new WordList(
this.words.filter((x) => {
const my = Score.of(x, score.guess)
return my.marks.join(",") == score.marks.join(",")
}),
)
}
toString() {
return this.words.join(", ")
}
filter(
predicate: (word: Word, index: number, array: WordList) => unknown,
): WordList {
const p = (word: Word, index: number) => predicate(word, index, this)
return new WordList(this.words.filter(p))
}
best() {
return this.words
.map((guess) => {
const filtered = this.matchesWord(guess)
return [guess, filtered.words.length] as const
})
.reduce((a, b) => (a[1] <= b[1] ? a : b))
}
bestSingleLayerGuesses() {
return this.words
.map((guess) => {
const filtered = this.matchesWord(guess)
return [
guess,
filtered.words.length,
filtered.toString(),
] as const
})
.sort(([, a], [, b]) => a - b)
}
best2() {
return this.words
.flatMap((guess1) => {
const f1 = this.matchesWord(guess1)
return f1.words.map((guess2) => {
const f2 = f1.matchesWord(guess2)
return {
guess1,
guess2,
size: f2.words.length,
words: f2.toString(),
}
})
})
.reduce((a, b) => (a.size <= b.size ? a : b))
}
bestSecondLayerGuesses() {
return this.words
.flatMap((guess1) => {
const f1 = this.matchesWord(guess1)
return f1.words.map((guess2) => {
const f2 = f1.matchesWord(guess2)
return {
guess1,
guess2,
size: f2.words.length,
words: f2.toString(),
}
})
})
.sort((a, b) => a.size - b.size)
}
best4() {
return this.words
.flatMap((guess1) => {
const f1 = this.matchesWord(guess1)
return f1.words
.filter((guess2) => guess2 >= guess1)
.map((guess2) => {
const f2 = f1.matchesWord(guess2)
return {
guess1,
guess2,
f2,
size: f2.words.length,
}
})
})
.sort((a, b) => a.size - b.size)
.slice(0, 100)
.flatMap(({ guess1, guess2, f2 }) => {
return f2.words
.filter((guess3) => guess3 >= guess2)
.map((guess3) => {
const f3 = f2.matchesWord(guess3)
return {
guess1,
guess2,
guess3,
size: f3.words.length,
f3,
}
})
})
.sort((a, b) => a.size - b.size)
.slice(0, 100)
.flatMap(({ guess1, guess2, guess3, f3 }) => {
return f3.words
.filter((guess4) => guess4 >= guess3)
.map((guess4) => {
const f4 = f3.matchesWord(guess4)
return {
guess1,
guess2,
guess3,
guess4,
size: f4.words.length,
}
})
})
.reduce((a, b) => (a.size < b.size ? a : b))
}
bestThirdLayerGuesses() {
return this.words
.flatMap((guess1) => {
const f1 = this.matchesWord(guess1)
return f1.words
.filter((guess2) => guess2 >= guess1)
.map((guess2) => {
const f2 = f1.matchesWord(guess2)
return {
guess1,
guess2,
f2,
size: f2.words.length,
words: f2.toString(),
}
})
})
.sort((a, b) => a.size - b.size)
.flatMap(({ guess1, guess2, f2 }) => {
return f2.words
.filter((guess3) => guess3 >= guess2)
.map((guess3) => {
const f3 = f2.matchesWord(guess3)
return {
guess1: guess1.toString(),
guess2: guess2.toString(),
guess3: guess3.toString(),
size: f3.words.length,
words: f3.toString(),
}
})
})
.sort((a, b) => a.size - b.size)
}
simulateAll(initial: Word) {
let allOk = true
const all: [string, number][] = []
for (const correct of this.words) {
const { attempts, log, ok } = simulate(correct, this, initial)
all.push([correct.toString(), attempts])
if (ok) {
console.groupCollapsed(
`Guessed "${correct}" in ${attempts} tries.`,
)
for (const phrase of log) {
console.log(phrase)
}
console.groupEnd()
} else {
allOk = false
console.groupCollapsed(`Failed to guess "${correct}".`)
for (const phrase of log) {
console.log(phrase)
}
console.groupEnd()
}
}
return {
word: initial.toString(),
max: all.reduce((a, b) => (a > b[1] ? a : b[1]), -Infinity),
avg: +(all.reduce((a, [, b]) => a + b, 0) / all.length).toPrecision(
5,
),
ok: allOk,
data: all,
}
}
simulateAllWithAllInitials() {
const { log, group, groupEnd } = console
console.log = () => {}
console.group = log
console.groupEnd = () => {}
const data = this.words
.map((word) => this.simulateAll(word))
.sort((a, b) => a.avg - b.avg)
Object.assign(console, { log, group, groupEnd })
return data
}
tsv() {
const data = this.simulateAllWithAllInitials()
return data.map(
(initial) =>
initial.word + "\t" + initial.data.map((x) => x[1]).join("\t"),
)
}
}
export const all = new WordList(
"anpa ante awen esun insa jaki jelo kala kama kasi kili kule kute lape laso lawa leko lete lili lipu loje luka lupa mama mani meli meso mije moku moli musi mute nasa nena nimi noka olin open pali pana pini pipi poka poki pona sama seli selo seme sewi sike sina soko sona suli suno supa suwi taso tawa telo toki tomo unpa walo waso wawa weka wile"
.split(" ")
.map((word) => Word.of(word)),
)
export function simulate(correct: Word, list: WordList, initial: Word) {
if (!list.includes(correct)) {
throw new Error("Correct word is not in allowed word list.")
}
if (!list.includes(initial)) {
throw new Error("Initial guess is not in allowed word list.")
}
let i: Word | undefined = initial
const log: string[] = []
let attempts = 0
log.push(`Starting with ${list.words.length} words...`)
while (list.words.length > 1) {
const guess =
i
|| (() => {
const b = list.best4()
return (
[
[b.guess1, list.matchesWord(b.guess1)],
[b.guess2, list.matchesWord(b.guess2)],
[b.guess3, list.matchesWord(b.guess3)],
[b.guess4, list.matchesWord(b.guess4)],
] as const
).reduce((a, b) =>
a[1].words.length < b[1].words.length ? a : b,
)[0]
})()
i = undefined
const score = Score.of(correct, guess)
list = list.matchesScore(score)
log.push(`Guessed "${guess}". ${list.words.length} words left.`)
attempts += 1
}
if (list.words.length == 0) {
log.push("Word list is empty.")
return { log, ok: false, attempts }
}
const final = list.words[0]!
if (!final.equals(correct)) {
log.push(
`"${final}" is the only remaining option, but it is incorrect.`,
)
return { log, ok: false, attempts }
}
log.push(`"${final}" is the only remaining option.`)
log.push("Done.")
return { log, ok: true, attempts }
}