Wordle 2: writing an automated solver

First, set the browser to Full Screen view, to give this worksheet and your code as much space as possible.

Then choose either one of the two options below:

Option 1: Start this worksheet from scratch

Option 2: Continue, or view, previous work

This worksheet is copyright © Richard Pawson 2025, and protected by the Creative Commons license: Attribution-NonCommercial-NoDerivatives 4.0 International. You may freely make and distribute copies of this worksheet as is, but if you modify this worksheet you may not distribute your modified version (outside your own teaching institution) without the author's permission. Please email the author to report errors or suggest improvements.

Step 1: introduction

Following-on from 'Wordle 1: implementing the Wordle puzzle logic', in this worksheet your challenge is to implement a reverse game: where you choose the target word, and the computer is given up to six attempts to identify the word, with you marking the letters of each attempt as 'grey', 'yellow', or 'green' - using the 0, 1, and 2 keys, respectively. As in Wordle 1, you will start off with some working code that implements the presentation and user-interaction needed for the task.

Before we start, you need to understand the following points, which apply both to this implementation and also to the official online Wordle puzzle:

Hint 1-1: key ideas
Hint 1-2: the index
Hint 1-3: the expression to which 'attempt' needs to be set
Hint 1-4: the whole, modified, procedure

When you have implemented this change, run the program and check that it is using a different attempt word each time (though, as the attempt word is chosen at random, there is a small chance of repeating the same word). However, the likelihood that the computer gets to the target within the maximum six attempt is still very low. For the next step we need to implement a viable strategy.

Notes

Total hints used: /

Step 2: Start to implement a simple viable strategy

Human players use many different strategies to choose their next attempt word, based on the marks given for previous attempts. However, one of the simplest is to pick any new word that would be consistent with the marks given. In other words: if the word we choose as our next attempt were the target word, then our previous attempts would still have received the marks they did receive.

We could make that choice from any of the 15,000+ words in the list allValidAttemptWords, but we are going to select our next attempt from just the 2,315 allValidTargetWords as this should increase the chances of identifying the target. (Incidentally, by adopting this strategy we are actually playing Wordle in 'hard' mode.)

Hint 2-1: defining the function signature
Hint 2-2: defining the stub implementation
Hint 2-3: the complete function so far

Having written the stub implementaton of possibleTargetsAfterAttempt the tests will run. Each assert instruction defines a different 'test case'.

How many of the test cases are now passing, and what do they have in common?

Notes

Total hints used: /

Step 3: Completing the implementation

The next task is to implement possibleTargetsAfterAttempt such that all the associated test cases pass.

If you have learned about 'higher order functions', you can apply those techniques here and implement the function as a single instruction. However, the Hints below assume that that you will be implementing the code more conventionally, using a loop.

Hint 3-1: initial ideas
Hint 3-2: outline of new instructions
Hint 3-3: two useful expressions
Hint 3-4: The complete function

Before marking this step as completed, make sure that all the test cases are now passing.

Notes

Total hints used: /

Step 4: Making use of the filtered list of possible target words

Now that we have the ability to filter the list of possible target words based on each new marked attempt we can make use of that with the playReverseGame procedure.

Insert a new instruction immediately after instruction 69 i.e. just above the instruction that you added into the procedure earlier. This new instruction should set the list named possible to the new, filtered, list that results from evaluating the possibleTargetsAfterAttempt function, passing in suitable arguments.

Hint 4-1: the expression to evaluate the filtered version of the list
Hint 4-2: complete procedure so far

Once you have implemented this change, you should be able to run the program. For the first run, mark the computer's attempts against the same target word that we used at the start of this worksheet: STAGE.

Does the computer get to the word within six attempts?

Now run the puzzle three more times - choosing a different target word in each case. Important: you can pick any well-known five letter word - provided that it is not a shorter word to which S or D has been appended (the official Wordle puzzle never sets such words as the target, either).

Record below the three target words that you selected and, in each case write down the number of attempts the computer took to solve the puzzle, or 'failed' if it did not solve it within six attempts.

QURSH is a real word, and may be used as an attempt word but it is not well-known and is never going to be set as a target word in the official game. If you pick this word (or one of many other disallowed target words), then this 'reverse player' will not be able to solve the puzzle. Try it.

What actually happens?

Notes

Total hints used: /

Step 5: Two small refinements

We will make these two small refinements in one go:

So...

Hint 5-1: outline version of the new instructions
Hint 5-2: Complete revised procedure

When you have completed these changes, run the program and test it: three times against different valid target words and once against QURSH or another very obscure 5-letter word.

Note that you might also the the message "No possible target matches marks so far" if you mark any attempts incorrectly.

Notes

Total hints used: /

Congratulations! Worksheet completed

You have just implemented the core logic for an automated Wordle solver. Try going to the official Wordle site and (assuming you have not already solved today's Wordle puzzle) see if your solver can solve today's target word in 6 or fewer attempts. To do this, just enter the computer's attempt word into the official puzzle, and after that has been marked, replicate the marks on your solver.

If you would like to share the working application with others, click file > save as standalone to save it as a self-contained single web page that shows only an enlarged version of the display tab runs the program automatically when it is opened, and which runs automatically when opened. This standalone email file may be emailed to others.

Does this spoil the fun of doing Wordle? Not at all! The author of this worksheet solves the official online Wordle puzzle every day, and only then tests his automated Wordle solver, to see whether he beats the computer, or vice versa.

There is another worksheet available - Wordle 3 - in which you write code to analyse the effectiveness of this wordle solver, rigorously, and then suggests ways in which you could explore simple and more complex variants on the algorithm implemented here.

If you wish, you may record any feedback or further notes here:

Meantime, if you have time, explore any of the following suggestion optional extensions to the current code.