Last active
January 25, 2022 23:06
-
-
Save treyhoover/1e6a51800b907bc53e4b4f8656b68a30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dictionary = require("./words.json"); | |
class Wordle { | |
static DEFAULT_ALPHABET = Array.from({ length: 26 }, (_, i) => String.fromCharCode(i + 97)); | |
static DEFAULT_DICTIONARY = []; | |
static DEFAULT_SIZE = 5; | |
static calcLetterFrequency(words = []) { | |
return words.reduce((freqs, word) => { | |
for (const letter of word) { | |
freqs[letter] = (freqs[letter] ?? 0) + 1; | |
} | |
return freqs; | |
}, {}); | |
} | |
constructor({ alphabet, dictionary, size } = {}) { | |
this.dictionary = dictionary ?? Wordle.DEFAULT_DICTIONARY; | |
this.size = size ?? Wordle.DEFAULT_SIZE; | |
this.alphabet = alphabet ?? Wordle.DEFAULT_ALPHABET; | |
this.letterOptions = Array.from({ length: this.size }, () => new Set(this.alphabet)); | |
this.required = []; | |
} | |
get possibilities() { | |
return new Set(dictionary.filter(word => { | |
for (let i = 0; i < word.length; i++) { | |
if (!this.letterOptions[i].has(word[i])) return false; | |
} | |
return this.required.every(letter => word.includes(letter)); | |
})) | |
} | |
get scoredPossibilities() { | |
const possibilitiesArr = Array.from(this.possibilities); | |
const freqs = Wordle.calcLetterFrequency(possibilitiesArr); | |
return possibilitiesArr.map(word => { | |
const uniqLetters = Array.from(new Set(word.split(''))); | |
const score = uniqLetters.reduce((sum, letter) => sum + freqs[letter], 0); | |
return { | |
word, | |
score, | |
} | |
}).sort((a, b) => b.score - a.score); | |
} | |
doesNotInclude(letter) { | |
for (const options of this.letterOptions) { | |
options.delete(letter); | |
} | |
return this; | |
} | |
mustInclude(letter, { at, not } = {}) { | |
this.required.push(letter); | |
if (at > -1) { | |
this.letterOptions[at] = new Set([letter]); | |
} | |
if (not > -1) { | |
this.letterOptions[not].delete(letter); | |
} | |
return this; | |
} | |
couldBe(word) { | |
return this.possibilities.has(word); | |
} | |
} | |
const wordle = new Wordle({ dictionary }); | |
wordle | |
.mustInclude('a', { not: 0 }) | |
.mustInclude('r', { not: 1 }) | |
.doesNotInclude('o') | |
.mustInclude('s', { not: 3}) | |
.doesNotInclude('e') | |
.mustInclude('r', { not: 0 }) | |
.mustInclude('a', { not: 1}) | |
.doesNotInclude('i') | |
.doesNotInclude('t') | |
.mustInclude('s', { not: 4 }) | |
.mustInclude('s', { at: 0 }) | |
.doesNotInclude('c') | |
.mustInclude('a', { not: 2 }) | |
.mustInclude('r', { not: 3 }) | |
.doesNotInclude('p') | |
.mustInclude('u', { at: 1 }) | |
.mustInclude('r', { not: 2 }) | |
.mustInclude('a', { at: 3 }) | |
.doesNotInclude('h') | |
console.log(wordle.scoredPossibilities); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment