Introduction: Win at Wordle

If you read this Instructables, I guess that you probably already know what Wordle is... If not, Wordle is an online word game, with a new puzzle every day. You have 6 chances to find a five-letter word, in English. After your guess, the color of each letter indicates whether it is well placed (green), it exists but in another spot (yellow) or it is not in the word (grey).

To play, just go here: https://www.powerlanguage.co.uk/wordle/

After making a first Instructables with an ESP32 code to help practice your game and improve your strategy, I decided to see if it was possible to design a computer code able to win at Wordle. I ended up with a Python script which is very efficient. I tested it against the practicing code, and then online and it reaches very good results : the word is found is 4 to 5 guesses, sometimes 6 when the target word has several words that look alike it.

One short diclaimer: this was not done for cheating and is not supposed to allow you to cheat. The only purpose of that Instructables was to explore the possibility of finding a winning strategy for this game. There have been many similar works in more than 50 years, targetting other well known games, such as Tic Tac Toe, checkers, Othello, Mastermind, checks and go. But none of these works have ever prevented anyone from having fun in playing those games.

So you can use this code, but the most fun way of playing is to practice and improve your skills by yourself.

Supplies

You just need a computer and a Python execution environment.

The code is dependant on a library, called texdistance To install it, type:

pip install textdistance

Download the files below. You can also find them on my Github repo (I'll put any updates there).

The txt files are 5 letter words dictionnaries, in English and in French. Put all the files in the same folder on your computer. If you want to change the language, comment or uncomment the lines:

  filename = "Dict-5757.txt"     # uncomment for english game
  # filename = "Dict-FR-7980.txt"   # uncomment for french game

The above example is to play in English.

Step 1: How to Play?

First go to the Wordle website and choose a first guess This part is important as the first guess may provide information that is fundamental for the rest of the game. There are many sites that deal with the choice of the first guess. I think that the first guess should use as many vowels as possible (see step 2). For example:

  • about
  • above
  • adieu

Type your first guess in the Wordle game and get its answer (score).

Then run the python script. It first asks for the word you just tried. Then it asks for the score, meaning the answer from Wordle. As the answer is colored, I used the same convention as in my other Instructables:

  • + for a letter that is correctly placed (green)
  • = for a misplaced letter (yellow)
  • - for a letter that is not in the target word (grey)

For example, if the answer was : green yellow grey grey green, you just need to type +=--+

Be careful, as there is no verification. My code does not verify that you typed a 5-letters word or 5 characters for the answer.

Next, just follow the instructions from the code. If everything goes well, the code suggests a new word for you to play. Type it in Wordle, and then enter Wordle's answer, and so on until you win...

If the suggested word is not recognized by Wordle, then type 0 as the answer: the code will provide more suggestions. Choose one of them, type it both in Wordle and in the code, and continue as before.


Here is an example from today (Jan. 29, 2022):

  • First guess is above (the word 'above')
  • Wordle's answer: grey grey yellow grey grey, which is translated to --=--
  • Suggestion: ohhhh
  • This word is not recognized by Wordle
  • Input 0
  • More suggestions: would words could sound found
  • I choose words
  • Wordle's answer: -+-=-
  • Suggestion: doggo
  • Answer: =+---
  • Suggestion: mound
  • Answer: -++-+
  • Suggestion: could
  • Found it! Answer is +++++

See the images for a more graphical explanation.

You can see that 'could' was one the suggestions of the code, when 'ohhhh' was not recognized: I could have won in 2 guesses!!

Step 2: How Does It Work?

Before programming, I tried to find similar works. The Wordle game is quite similar to the MasterMind game, so I first looked for any research for a winning strategy at this game. Donald Knuth, an american mathematician and computer scientist actually found one which is called the Five Guess Algorithm.

He has shown that there exists a winning strategy and that a specific first guess ensures that any target combination can be found in no more than 5 guesses. If you want to learn more about it, there is an article he wrote available online.

I won't explain this in details as I just used it for inspiration, but I learned that the first guess is very important in the rest of the game. Knuth used a minimax kind of strategy, in which each guess tries to minimize the number of possible winning guesses. More precisely, it minimizes the score of each possible guess, the score being the maximum number of combinations of colors eliminated in the set of all possible results (all possible combinations of black and white pegs).


The similarity with Wordle is only superficial, as the words used in Wordle must be real English words. This is why we need a dictionary (merely a list of valid 5 letter words), the dictionary in Mastermind has only 1296 "words". The answers from Mastermind only deal with correctly placed and misplaced colors, not with uncorrect colors.

So I decided to simplify Knuth's algorithm. For each guess, my algorithm lists the words in the dictionary that would provide the same answer. It then retains the intersection of this set and the current set obtained from the previous guesses. This is efficient enough to quickly reduce the number of possible solutions around 10 or less in 3 or 4 guesses.


You can verify this in the example from step 1. The dictionary contains 5757 words, so there are a priori 5757 possible solutions. At the first guess, this number reduces to 432, then to 19 then to 7 after 3 guesses. After the 4th guess, there remains only one possible answer, which is the word we are looking for.

In a first version of the code, the suggestion made was the first one of the set in alphabetical order. This was a bias, and sometimes, this word wasn't recognized by Wordle (see the example in step 1). To reduce the bias, I looked for another suggestion strategy. I opted for the choice of the word that maximizes the distance to the previous guess. The distance is calculated using the library 'textdistance' that you installed before. Among many available distances in this library, I chose the one called 'entropy_ncd' as it was the one which provided the most variations in the distance values. This strategy reduces the bias.

When the suggested word is not recognized by Wordle, I added the possibility of providing other suggestions for you to choose.


Of course there may exist better strategies than this one, and I'd be happy to see if anyone can provide a more efficient one. Maybe artificial intelligence can help here, as there are many AI works linked to decision making.

Step 3: A New Win in French

And here is a win in 3 guesses for the French version of the game.

Step 4: More Games to Play

A new version is available on the github, which enables to play new variants, such as Absurdle and Quordle.

Have fun!

Anything Goes Contest 2021

Participated in the
Anything Goes Contest 2021