Cheating at Wordle

Dictionary

If we are going to work on some kind of application to help us cheat at wordle, we need a dictionary of possible words.

In this case it seems that the game-makers over at the New York Times maintain two sets of words. There are words that can be answers to the daily Wordle puzzle, and there are words that will be accepted as valid guesses.

Fortunatly there is no shortage of suitable dictionaries online. I copied mine, from Giorgio Gilestro's Laboratory. For the interested reader, there is a quick writeup on Wordle strategies on his laboratory blog.

Playing the Game

At the start of the game, the player knows nothing about the day's word.

It is reasonable to suggest that some words may make better first guesses than others.

Words that test the maximum number of different letters, will in general provide more information about the day's word.

Words that contain more common letters are more likely to strike a match, but in general will elimate fewer possibilities.

Let us suppose that when the word of the day is "TRAIT", that our first guess is "REACT".

Wordle provides feedback by coloring the letters of the guess.

The simplest case is letters are colored green to indicate that they are correct.

Since letters 3 and and 5 are coloured green, this means that letter 3 must be 'A', and letter 5 must be 'T'.

Letter 1 is yellow. This indicates that letter 1 is not "R", but at least one of the other letters must be "R". Since letters 3 and 5 are determined, that means either letter 2 or 4 must be "R".

Letters are colored grey to indicate that they did not match the corresponding letter of the answer.

grep -E "^[^ecr][^ec]a[^ec]t$" words-2.txt | grep "r" 

This reduces the possible answers to one of the list:

apart, draft, graft, grant, quart, smart, start, trait

Comparison Algorithm

const wordLength = 5;

function wordArray(word)
{
    const wordLower = word.toLowerCase();

    const chars = Array.from(wordLower);

    return chars;
}

function countCharacters(word)
{
    const wordChars = wordArray(word);

    var count = {};

    for (var char of wordChars)
    {
        count[char] = (count[char]?count[char]:0) + 1;
    }

    return count;
}

function compareWords(actual, guess)
{
    const actualChars = wordArray(actual);

    const guessChars = wordArray(guess);

    let actualCount = countCharacters(actual);

    let comparison = Array(wordLength).fill('-');

    for (let index = 0; index < wordLength; index++)
    {
        if (actualChars[index] === guessChars[index])
        {
            comparison[index] = '+';
            actualCount[guessChars[index]]--;
        }
    }

    for (let index = 0; index < wordLength; index++)
    {
        if (actualChars[index] !== guessChars[index])
        {
            if (actualCount[guessChars[index]] > 0)
            {
                comparison[index] = '?';
                actualCount[guessChars[index]]--;
            }
        }
    }   

    return comparison;
}

console.log(`countCharacters("TRAIT")=${JSON.stringify(countCharacters("TRAIT"))}`);


console.log(`compareWords("TRAIT", "REACT")=${compareWords("TRAIT", "REACT")}`);

After each guess the game provides information that narrows the list of possible answers.

This information is provided by colour coding the letters of the players guesses.

Each letter is colour coded either green, yellow or grey.

If a letter is coloured green, that indicates that the answer contains that letter in the possition that is indicted.

A Little More Precise

Let \(W_S\) be the set of words that can be the solutions to a Wordle puzzle and \(W_G\) be the set of words that are accepted as guesses.