Build a Wordle Solver Using Rust

You beat my grandmother in Wordle – here’s why you should care

Photo by Ravi Patel on Unsplash. Not my grandmother

the game

Wordle is a relatively simple game. If you’ve played Mastermind before, it should look familiar. The goal is to discover a mysterious word with as few guesses as possible. The word mystery changes every day. Here are two examples of guesses.

Two Examples of Wordle guesses

After guessing, the color of each letter changes.

  • Green – the message is correct.
  • Yellow—the letter is in the word, but not in that space.
  • Gray – the letter is not in the word.

As you can see, there are a maximum of six guesses. If you can’t find the mystery word within six guesses, you lose. I’ve been competing with my grandmother every day to find the word in the fewest guesses possible.

XKCD 365: The Hunting Nerd.
XKCD 356: Nerd Sniping

This nerd totally sniped me. I felt a great desire to create an app that could find the mystery word in as few guesses as possible.

how did you do that

First things first, we need a list of English words. I used the corncob menu initially, but found more success with Doyle’s menu.

For this project, I decided to use Rust, only because I felt more confident in my ability to quickly create an MVP.

Each of the word lists I used was formatted as a sequence of individual words, separated by n characters. On Windows (which is what I use), they also have these annoying lowercase letters.

Wordle places great emphasis on letters. I can remove items from the word list based on the letters I know are not in the obscure word (these are gray in-game characters), and I can remove items based on the letters I know be In the word is ambiguous (orange or green letters), but in many cases that still leaves a lot of possible words. I need a way to sort words based on how likely their letters are to appear.

To do this, I count the number of times each letter appears in the word list, and give each word a score based on how frequently its components appear.

The first step in the program is to load the word list and count the letters:

Using the default HashMap (which uses SipHash, which isn’t great for searching for a single character) might not be the best, in terms of performance, but it’s just a game program, and doesn’t have to be the fastest thing in the world.

Next, we need to go through the word list, and remove the words containing gray letters. This function helps do that:

It accepts a few different character slices:

  • Word: The word we want to check.
  • Found: This is a slide containing the characters we found (green letters), with “” (space) characters in locations whose character we don’t know.
  • No: This is a slide that contains the letters we know it’s not in the ambiguous word.
  • Should: This is a slide that contains the character we know be In the obscure word, but we do not know the niche.
  • Masks: a series of masks. We remove every word containing letters that matches any mask here. This is useful for removing words containing punctuation in a word list and for removing words containing orange letters, but in positions where we know they are not.

Now all we have to do is run each word in the word list and see if it matches our already known letters, and update the contents of each slide with new information after each guess.

Why should you care

This seems like a useless problem. that it. There’s no way that anyone else but me would benefit, and I certainly wouldn’t use this when I’m already competing with my grandmother.

Then why did you do that?

Useless answers to useless problems are useful. They teach us how to develop without the stress of real bets. They are also just plain fun.

It is also a reflection. How would you have dealt with this problem in the past? How to improve your thinking. It might be a bit cool to say that this little Wordle is the key to self-reflection, but I don’t think that’s too far fetched.

Want to Connect?
If you like art, or photography, also check out my Instagram.

Creating Wordle Solver with Rust was originally published at Better Programming on Medium, where people continue the conversation by highlighting and responding to this story.

Leave a Comment