Skip to content

Instantly share code, notes, and snippets.

@erickmerchant
Last active April 10, 2022 14:34
Show Gist options
  • Save erickmerchant/2660bf9e7459fad7033750d33d05da92 to your computer and use it in GitHub Desktop.
Save erickmerchant/2660bf9e7459fad7033750d33d05da92 to your computer and use it in GitHub Desktop.
wordle helper
const maskAll = (word, letter) => {
let mask = 0;
let i = -1;
while (
(i = word
.split("")
.reverse()
.join("")
.indexOf(letter, i + 1)) != -1
) {
mask |= 1 << i;
}
return mask;
};
const text = await Deno.readTextFile("./words.txt");
const words = text.split("\n");
const filtered = words.filter((word) => {
const letters = {};
for (const [letter, { yellow = 0, green = 0, gray = 0 }] of Object.entries(
letters
)) {
const mask = maskAll(word, letter);
if (gray && mask & gray) return false;
if (yellow && mask & yellow) return false;
if (yellow && mask === 0) return false;
if (green && (mask & green) !== green) return false;
}
return true;
});
const counts = [{}, {}, {}, {}, {}];
for (const word of words) {
const chars = word.split("");
for (let i = 0; i < chars.length; i++) {
const char = chars[i];
counts[i][char] ??= -1;
counts[i][char]++;
}
}
const ranked = {};
for (const word of words) {
const chars = word.split("");
ranked[word] = 0;
for (let i = 0; i < chars.length; i++) {
const char = chars[i];
ranked[word] += counts[i][char];
}
}
filtered.sort((a, b) => ranked[b] - ranked[a]);
for (const word of filtered.sort((a, b) => ranked[b] - ranked[a])) {
console.log(word, ranked[word]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment