For context Wordle is game where you have to decipher a 5 letter word in 6 or less guesses based on certain hints. The hints you get are as follows:
I am making a wordle solver program that takes in an array of word attempts and eliminates them from a list of the possible words.
I feel that the best algorithm to solve this problem is a black list where a word that breaks one of the rules is eliminated from the array. But if there is a better alternative I am open to suggestion.
const text =
[
[
["N","black"],["i","black"],["g","black"],
["h","black"],["t","green"]
],
[
["b","black"],["e","black"],["l","orange"],
["o","orange"],["w","black"]
]
]
const words = "dozen,brave,apple,climb,outer,pitch,ruler,holds,fixed,costs,calls, ...etc"
const solver = (text: any) => {
this.resultingWords = words.split(",").filter(word => {
word = word.toUpperCase()
for (var i = 0; i < text.length; i++) {
for (var j = 0; j < 5; j++) {
let currentText = text[i][j]
currentText[0] = currentText[0].toUpperCase()
if (currentText[0] == '') { continue }
if (currentText[1] == "green" && (word[j] != currentText[0])) {
return false
}
if (currentText[1] == "black" && word.includes(currentText[0])) {
return false;
}
if (currentText[1] == "orange" &&
(word[j] == currentText[0] || !word.includes(currentText[0]))) {
return false
}
}
}
return true
})
}
The issue I am having is if a word has multiple of the same letter and one of them is a green or orange match but the other is black. I get no results because of the way I've written my algorithm.
What would be the way to correctly solve this problem?
Is a black list style of filtering the best solution? (as opposed to white list).
You are building a list of candidates, what is a good start I think. It doesn't really matter whether you white- or blacklist the result is the list of candidates. The only concern is that you could get the solution faster or more reliably by guessing words that are not on the candidates list. Why? Because that way you can introduce more new letters at once to check if the word contains them. Maybe a mix between the two strategies is best, hard to tell without testing it first.
There are a lot of ideas for improvements. First I would create the filter before going through the words. Using similar logic as above you would get a collection of four different rule types: A letter has to be or cannot be at a specific position or a letter has to appear exactly (possibly 0) or at least a specific number of times. Then you go through the words and filter using those rules. Otherwise some work might be done multiple times. It is easiest to create such a filter by collecting the same letters in the guess first. If there is an exact number of appearence rule then you can obviously drop a minimal number of appearance rule for the same letter.
To guess the word fast I would create an evaluation function to find the most promising next guess among the candidates. Possible values to score: