Wordle With Grep

By Susam Pal on 22 Jan 2022

Let us solve a couple of Wordle games with the Unix grep command and the Unix words file. The Wordle games #217, #218, and #219 for 22 Jan 2022, 23 Jan 2022, and 24 Jan 22, respectively, are used as examples in this post. The output examples shown below are obtained using the words file /usr/share/dict/words, GNU grep 3.6, and GNU bash 5.1.4 on Debian GNU/Linux 11.2 (bullseye).

Note that the original Wordle game uses a different word list. Further, there are several Wordle clones which may have their own word lists. For the purpose of this post, we will use the word list that comes with Debian. We will solve each Wordle in a quick and dirty manner in this post. The focus is going to be on making constant progress and reaching the solution quickly with simple shell commands.

Contents

Preliminary Work

Before we start solving Wordle games, we will do some preliminary work. We will create a convenient shell alias that automatically selects all five-letter words from the words files. We will also find a good word to enter as the first guess into the Wordle game. The following steps elaborate this preliminary work:

  1. Make a shell alias named words that selects all 5 letter words from the words file.

    $ alias words='grep "^[a-z]\{5\}$" /usr/share/dict/words'
    $ words | head -n 3
    abaci
    aback
    abaft
    $ words | tail -n 3
    zoned
    zones
    zooms
    $ words | wc -l
    4594
    
  2. For each letter in the English alphabet, count the number of five-letter words that contain the letter. Rank each letter by this count.

    $ for c in {a..z}; do echo $(words | grep $c | wc -l) $c; done | sort -rn | head -n 15
    2245 s
    2149 e
    1736 a
    1404 r
    1301 o
    1231 i
    1177 l
    1171 t
    975 n
    924 d
    810 u
    757 c
    708 p
    633 h
    623 y
    

    The output shows that the letter 's' occurs in 2245 five-letter words, followed by 'e' which occurs in 2149 five-letter words, and so on.

  3. Find a word that contains the top five letters found in the previous step.

    $ words | grep s | grep e | grep a | grep r | grep o
    arose
    

    We will enter this word as the first guess in every Wordle game.

  4. In case, the word "arose" does not lead to any positive result, we will need another word to enter as our second guess. Find a word that contains the next five top letters in the list found above.

    $ words | grep i | grep l | grep t | grep n | grep d
    $ words | grep i | grep l | grep t | grep n | grep u
    until
    

    We found that there is no such word that contains 'i', 'l', 't', 'n', and 'd'. So we got rid of 'd' in our search and included 'u' (the next highest ranking letter after 'd') instead to find the word "until". We will enter this word as the second guess if and only if the first guess (i.e., "arose") does not lead to any positive result.

Wordle #217

Let us now solve Wordle #217 for Sat, 22 Jan 2022 with the following steps:

  1. Use the word "arose" as the first guess. The following result appears:

    A R O S E

  2. The previous result shows that the letter 'e' occurs at the fifth place. Further, the letters 'a', 'r', 'o', and 's' do not occur anywhere in the word. Look for words satisfying these constraints.

    $ words | grep '....e' | grep -v '[aros]' | head -n 5
    beige
    belie
    belle
    bible
    bilge
    

    Pick the word "beige" for the second guess and enter it into the Wordle game. Note that since we are following a quick and dirty approach here, we do not spend any time figuring out which of the various five-letter words ending with the letter 'e' is the most optimal choice for the next guess. We simply pick the first word from the output above and enter it as the second guess. The following result appears now:

    B E I G E

  3. The letter 'i' occurs somewhere in the word but not at the third place. Further the letters 'b' and 'g' do not occur anywhere in the word. Also, the letter 'e' does not occur anywhere apart from the fifth place. The letter 'e' in the gray tile in the second place confirms that the letter 'e' does not repeat in the answer word. Refine the previous command to add these constraints.

    $ words | grep '[^e][^e][^ie][^e]e' | grep i | grep -v '[arosbg]' | head -n 5
    fiche
    indue
    lithe
    mince
    niche
    

    Enter "fiche" as the third guess. The following result appears:

    F I C H E

  4. The previous result shows that the letter 'i' occurs at the second place. Further, the letter 'c' occurs somewhere in the word but not at the third place. Also, the letters 'f' and 'h' do not occur anywhere in the word. Refine the previous command further to add these constraints:

    $ words | grep '[^e]i[^iec][^e]e' | grep c | grep -v '[arosbgfh]' | head -n 5
    mince
    wince
    

    Enter the word "mince" for the fourth guess. It leads to the following result:

    M I N C E

  5. We are almost there! We now have all the letters except the first one. The previous result shows that the letter 'm' does not occur in the word. Thus the answer word must be "wince". For the sake of completeness, here is a refined search that selects the answer word based on the constraints known so far:

    $ words | grep '[^e]ince' | grep -v '[arosbgfhm]' | head -n 5
    wince
    

    It looks like we have found the answer word. Enter "wince" as the fifth guess to get the following result:

    W I N C E

    Done!

Wordle #218

Now that the wordle for Sat, 22 Jan 2022 is solved, let us try the same method on Wordle #219 for Sun, 23 Jan 2022 and see how well this method works. Here are the steps:

  1. Like before, the first guess is "arose". Entering this word leads to the following result:

    A R O S E

  2. Now search for words based on the previous result.

    $ words | grep '.r...' | grep -v '[aose]' | head -n 5
    brick
    bring
    brink
    briny
    bruin
    

    Enter the word "brick" as the second guess. This leads to the following result:

    B R I C K

  3. Use the previous result to refine the search further.

    $ words | grep '.ri[^c].' | grep c | grep -v '[aosebk]' | head -n 5
    crimp
    

    Enter "crimp" as the third guess. This leads to the following result:

    C R I M P

    Done!

Wordle #219

Finally, let us solve Wordle #219 for Mon, 24 Jan 2022.

  1. Enter "arose" as the first guess to get this result:

    A R O S E

  2. The previous result shows that the third letter is 'o' and the letters 'a', 'r', 's', and 'e' do not occur anywhere in the word. Search for words that match these constraints.

    $ words | grep '..o..' | grep -v '[arse]' | head -n 5
    block
    blond
    blood
    bloom
    blown
    

    Enter "block" as the second guess. This leads to the following result:

    B L O C K

  3. The previous result shows that the letter 'l' occurs somewhere in the word but not at the second place. Similarly, the letter 'k' occurs somewhere in the word but not at the fifth place. Further, the letters 'b' and 'c' do not occur anywhere in the word. Search for words that match these constraints.

    $ words | grep '.[^l]o.[^k]' | grep l | grep k | grep -v '[arsebc]' | head -n 5
    knoll
    

    Enter "knoll" as the third guess. It leads to the following result:

    K N O L L

    Done!

Comments | #unix | #shell | #technology | #puzzle