Mike Kasberg

Husband. Father. Software engineer. Ubuntu Linux user.

Image for Solving Wordle with Programming

Solving Wordle with Programming

11 Jan 2022

Wordle is one of the latest crazes to hit Twitter. You might have noticed it – it’s the thing everyone’s posting with the yellow and green boxes. Wordle is a game where you have to guess the word of the day. Each time you guess, it’ll tell you if each letter appears at the spot you guessed, somewhere else in the word, or nowhere in the word. You win by guessing the correct word in 6 guesses or less.

⚠️ Spoiler Alert!

This blog post contains spoilers! It dissects the stragegy of Wordle, links to code that could help you cheat, and might ruin your fun. You've been warned!

Wordle is a fun problem to solve with your brain, but it’s also a fun problem to solve with a computer! It’s actually a great “intro to cryptography” type of problem, which is part of the reason I thought of trying to solve it with a computer in the first place. You see, Wordle is susceptible to a concept known in cryptography as frequency analysis. Some letters in the English language appear more frequently than others, and we can use that information to make better guesses in Wordle.

How do we turn this into a computer program?

Well, we start with a word list. I grabbed one from this GitHub repository. Once I had the word list, I wrote a quick script to filter it so that it only contained 5-letter words. (All words used in Wordle must be 5 letters.) That’s a pretty good starting point, but we can actually do better than that. I updated my script so that it would pre-sort the word list based on the combined frequency of the letters the word contains. This way, when we make our guesses, we can prioritize words that contain the most common letters.

Armed with this sorted word list, I wrote a script to solve Wordle. The script is pretty simple – about 100 lines of code! It always uses the same word to start, and this is actually the best strategy to use even if you’re solving a Wordle puzzle yourself with your brain. Spoiler alert! That word is serai – it contains the 5 most common letters in English. From there, using our word list, the script will eliminate words that don’t match, prioritize words with letters that do match but are in the wrong spot (yellow), and recommend another guess. The script tells you what to guess, you tell it the results, and it loops until you win (or lose).

$ ./solve.rb 
Welcome to the Wordle solver!
Use the following legend to enter the colored tiles.

Gray:   X
Green:  G
Yellow: Y

E.g. enter 'XXYGX' for a row that is gray, gray, yellow, green, gray.
Use 'NNNNN' if the solver suggests an invalid word.

Try this word: serai
Enter the Wordle result: XXYXY
Try this word: ronin
Enter the Wordle result: YXYYX
Try this word: iring
Enter the Wordle result: XGGGX
Try this word: print
Enter the Wordle result: XGGGX
Try this word: crink
Enter the Wordle result: NNNNN
Try this word: drink
Enter the Wordle result: GGGGG
Congratulations, you won!

I think there are probably a few small optimizations that could be made to the way it guesses words, but as it stands now I think I’ve already gotten 80% of the value with 20% of the work, and that’s good enough for me. I’ve only tested this with a handful of words so far, but it’s been able to solve each one. There are probably a few edge cases that will break something, but I can fix those as they pop up. Is this cheating? Yes, probably, but it was fun to build and that’s really the point. Don’t use it to beat your friends if you don’t want an unfair advantage. Want to see my code? It’s on GitHub.

About the Author

Mike Kasberg

👋 Hi, I'm Mike! I'm a husband, I'm a father, and I'm a senior software engineer at Strava. I use Ubuntu Linux daily at work and at home. And I enjoy writing about Linux, open source, programming, 3D printing, tech, and other random topics. I'd love to have you follow me on X or LinkedIn to show your support and see when I write new content!

Share!

If you enjoyed this blog post, I'd love it if you could share it with your network!

I run this blog in my spare time. There's no need to pay to access any of the content on this site, but if you find my content useful and would like to show your support, buying me a coffee is a small gesture to let me know what you like and encourage me to write more great content!

You can also support me by visiting LinuxLaptopPrices.com, a website I run as a side project.

Related Posts