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:
- If a character is coloured black there are no characters that match that character in the target word.
- If a character is coloured orange there is a character that matches that character in the target word but it is in a different position.
- If a character is coloured green the position and the character are a match.
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).