Solving WORDLE with grep

People seem to have become obsessed with wordle, just like they became obsessed with sudoku. After my initial burst of “oh a new game!” had waned, I was left thinking “my time is precious and this is exactly what we have computers for”. With this in mind, below is my quick and dirty way of solving these. I’m sure the regexp gurus amongst you will have a more elegant solution.

Step 1: Make sure you’ve got /usr/share/dict/words installed. This is just a huge list of words in a specific language and for me, this required installing the British words list.

sudo apt-get install wbritish

Step 2: Go to wordle

Step 3: Pick a random 5-letter word as your starting point. This is where grep and /usr/share/dict/words comes in:

grep -iE "^[a-z]{5}$" /usr/share/dict/words

Whilst this does somewhat look like a cat has slept on your keyboard, let’s break it down:

grep -modifier-flags "starts-with [pattern-of-letters] {repeated n times} ends-with" search-in-this-file
  • The -i flag makes grep search in a case-insensitive manner
  • The -E flag says extend grep’s normal regular expressions to support repeats
  • The ^ means the pattern has to be at the beginning of the word
  • [a-z] means search for all letters from a to z
  • {5} means that pattern we just saw (i.e. a to z), repeat it 5 times. This is the equivalent of doing ^[a-z][a-z][a-z][a-z][a-z]$
  • $ means the pattern needs to be at the end of a word too.
  • Finally, we’re specifying where we want to look for our pattern of characters

After all of that, I piped the output to head, to only see a few results, otherwise I’d get a screenful of every 5-letter word in the dictionary, all 6071 of them.

The first word in my list, Aaron isn’t in wordle’s list, so I need another one, in this case, I picked “ACTION”.

Result! I’ve got an O in the right place, the word contains a T (but it’s in the wrong place) and it does not contain an A C or N.

Step 4: Modify grep

grep -iE "^[a-z]{3}o[a-z]$" /usr/share/dict/words | grep --invert-match [a,c,n] | grep t

Grep is now searching for three letters, followed by an o, followed by another letter.

We then pass the results of this (222 words) into another grep but this time feeding grep the --invert-match flag. This does what it says on the tin. It only gives you results that do not contain the letters you’ve specified. In this case, we know our word doesn’t contain an A, C or N. Finally, since we know there’s a T, the final grep takes our 222 words and only finds us ones that contain a T, dropping our number of words down to 24.

Picking another random word out of my list, I went for DIVOT.

Progress. I now know the word ends in OT and does not contain A,C, N, D, I or V.

Step 5: Modify grep again

grep -iE “^[a-z]{3}ot$” /usr/share/dict/words |grep –invert-match [a,c,n,d,i,v]

This gave me the last seven words: Corot, Perot, begot, besot, helot, robot and shoot .

Having somewhat arbitrarily picked robot from that list, that’s all folks.

Postscript: In order to get the most bang for our buck in our initial search, we ideally want to search for a word where each character is unique. We could do this by using the lookahead function:

grep -P '^([a-z])(?!\1)([a-z])(?!\1|\2)([a-z])(?!\1|\2|\3)([a-z])(?!\1|\2|\3|\4)([a-z])$' /usr/share/dict/words

Author