Play Wordle like a pro with Python

Meesum Qazalbash
4 min readJun 20, 2022

--

Wordle logo
Wordle logo

We all want to play games like a pro. In the end, it rewards us with happy hormones in such a depressed society. Wordle Assistant is an open-source project that I worked on in the summer of 2022. It was written in C++ and Python. The repository may be found here. It is based on mathematical inference principles, which implies that if my guess contains a hint about the solution, we may deduce the probable responses to arrive at a more accurate guess.

Logic

Fortunately, we have the list of all words that are used in Wordle, thanks to Grand Sanderson. The intuition to solve this game lies in a simple method. First, all 26 alphabets are equally likely to be at any position. We make a guess. The guess will give some hint about the answer, using that hint we will cut the possibilities of having alphabets at those positions, along with a set of all allowed words — all green and yellow words.

We have simple rules according to the colour,

  1. If the colour is green, then only that letter can be at that position. Because only that letter can be at that place.
  2. If the colour is yellow then we will remove the letter from the corresponding position. Because the letter is in the answer but not at this place.
  3. If the colour is black, then we have two cases, If the letter is in allowed words then we will remove the letter from the corresponding position. If the letter is not in allowed words then we will remove the letter from all positions.

Keeping this rule in mind let's play a game and keep updating the map (for C++ guys) or dictionary (for Python guys) and see where we go. Before the first attempt, we have all alphabets equally likely to be at the position as we assume at first.

map = {0: {a,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z},
1: {a,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z},
2: {a,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z},
3: {a,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z},
4: {a,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,t,u,v,w,x,y,z}}
allowed_letters = {}
first guess

{b,o,s} will be removed from every position because they are not in the word at any position (they are not in allowed_letters). {e} is a set of green letters. Their corresponding location will only contain them, because we don’t want a word that has not a green letter at that position. {r} is a set of yellow words they will be remove form their corresponding location, because they are in the word but not at this position. After this move out map looks something like this.

map = {0: {a,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,t,u,v,w,x,y,z},
1: {a,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,t,u,v,w,x,y,z},
2: {a,c,d,e,f,g,h,i,j,k,l,m,n,p,q,t,u,v,w,x,y,z},
3: {e},
4: {a,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,t,u,v,w,x,y,z}}
allowed_letters = {e,r}
second guess

The same principles are employed to update the map, and the fact that a random word is chosen from the bucket of words that travel through the map indicates that they include the words that the map allows to appear at that position.

map = {0: {a,c,e,f,g,h,i,j,k,l,m,p,q,r,t,u,v,w,x,y,z},
1: {u},
2: {a,c,e,f,g,h,i,j,k,l,m,n,p,q,t,u,v,w,x,y,z},
3: {e},
4: {a,c,e,f,g,h,i,j,k,l,m,n,p,q,t,u,v,w,x,y,z}}
allowed_letters = {e,n,r,u}
third and final guess
map = {0: {n}, 1: {u}, 2: {d}, 3: {e}, 4: {r}}allowed_letters = {e,m,n,r,u}

I was able to win 8251 out of 10000 games with this technique. That is, nearly four out of every five games were won. The winning distribution is shown here.

{    "win": {        "1": 1,        "2": 84,        "3": 965,        "4": 2567,        "5": 2853,        "6": 1781    },    "loss": 1749
}
Stats of 10000 games

--

--

Meesum Qazalbash
Meesum Qazalbash

Written by Meesum Qazalbash

🌌 Exploring cosmic collisions as a senior undergrad student, crafting statistical methods for binary black hole mergers! 🚀🔍

No responses yet