Skip to content

Instantly share code, notes, and snippets.

@craigsdennis
Last active February 8, 2022 04:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigsdennis/46f95466db5da41e6b7184b04146bf1c to your computer and use it in GitHub Desktop.
Save craigsdennis/46f95466db5da41e6b7184b04146bf1c to your computer and use it in GitHub Desktop.
Wordle suggestion giver using Datamuse api
const axios = require("axios");
const WORD_LENGTH = 5;
// For testing, change these values
// You can make your own! https://mywordle.strivemath.com/?word=iclvp
const green = "?????";
const yellow = "eu";
const guesses = ["adieu"];
// End testing
const black = guesses
// To an array of arrays of letters
.map((word) => word.split(""))
// To a single array
.flat()
// Only the missing letters
.filter((letter) => {
return !yellow.includes(letter) && !green.includes(letter);
});
function findIndices(letter, word) {
return word
.split("")
.map((l, i) => {
if (l === letter) {
return i;
}
})
.filter((index) => index >= 0);
}
// Finds yellow places (right letter wrong space)
// Looks like {'e': [4, 3], 'a': [0]}
const yellowIndices = yellow.split("").reduce((indices, letter) => {
guesses.forEach((guess) => {
if (indices[letter] === undefined) {
indices[letter] = [];
}
const foundIndices = findIndices(letter, guess);
indices[letter] = indices[letter].concat(foundIndices);
});
return indices;
}, {});
// Datamuse query lang
// Looks like ?????,//eu???
const query = `${green},//${yellow}${"?".repeat(WORD_LENGTH - yellow.length)}`;
async function suggest() {
const response = await axios.get(
`https://api.datamuse.com/words?sp=${query}`
);
const allWords = response.data.map((object) => object.word);
// Filter out words that have black letters
const withoutBlackWords = allWords.filter((word) => {
return word.split("").every((letter) => !black.includes(letter));
});
// Filter out words that have yellow letters in the same position
const withoutIncorrectYellow = withoutBlackWords.filter((word) => {
// for each letter in the indices
for (const [letter, indices] of Object.entries(yellowIndices)) {
for (const index of indices) {
if (word.charAt(index) === letter) {
// Short circuit (Johnny 5 alive)
return false;
}
}
}
// It's a keeper!
return true;
});
console.log({ allWords, withoutBlackWords, yellowIndices, query, black });
return withoutIncorrectYellow;
}
suggest()
.then((suggestions) => console.log("Suggestions are", suggestions))
.catch((err) => console.error(err));
@craigsdennis
Copy link
Author

@elizabethsiegle I think this is getting close!

@elizabethsiegle
Copy link

TESTING LATER TONIGHT !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment