Improving my Wordle Score with scripting5 min read

So, the other day I wrote about how to use grep to solve wordle and I’ve been using that method to crush the scores of my coworkers, and so far, I’ve kept a 100% winning streak for the last 13 days.

However, what I realized is that every day I was doing a lot of menial work every day to get these answers. I was having to retype grep commands every day, and some of these grep commands got long:

grep -E "^([a-gi-km-oq-z]{1}[a-gi-z]{1}[b-z]{1}[a-z]{1}[a-gi-z]{1})$" /usr/share/dict/american-english | grep -Ev "([ousecugssuntbuncuncuncmuncevictexctejecterect])" | grep -E "([h])" | grep -E "([l])" | grep -E "([p])" | grep -E "([a])" | less

This became a pain and I had to type this command out every single guess. Occasionally I was able to hit up arrow and modify the command but more often I was retyping it by hand every time to make sure I got the script correct. Here’s a look at my bash history over the last several days.

This retyping every day took too much time and introduced too many errors. I’d tell grep to find a word with the letter E then also tell it to remove the letter E at the same time. I made too many errors. But I’m a tech guy and we have a phrase.

If I have to do something once, that sucks. If I have to do something twice, that sucks twice. If I have to do something three times, I’m scripting it.

So that’s what I did. This script took me about 2 hours to write and is certainly not efficient, but it works.

#!/bin/bash

echo "Welcome to the Wordle Solver."
echo "Please enter the first range of letters. (Note: enter them as a-z, or if you are eliminating letters then enter them as a range like a-df-z)"
read letter1
#echo "Your first quess is $letter1"
echo "Please enter the range of your second letter."
read letter2
echo "Please enter the range of your third letter." 
read letter3
echo "Please enter the range of your fourth letter."
read letter4 
echo "Please enter the range of your fifth letter."
read letter5
echo "Are there any letters you wish to eliminate? (Y/N)"
read answer1
grep -Ei "^([$letter1]{1}[$letter2]{1}[$letter3]{1}[$letter4]{1}[$letter5]{1})$" /usr/share/dict/american-english > ./wordleguesses.txt
cat wordleguesses.txt > wordleguesses2.txt
if [ $answer1 == Y ]; then
        echo "Enter ther letters to eliminate"
        read Eletters
        grep -Evi "([$Eletters])" wordleguesses.txt > wordleguesses2.txt
fi
echo "Are there any letters you know exist, but not the place? (Y/N)"
read answer
if [ $answer == Y ]; then
        echo "How many letters? (Enter 1-5)"
        read number_letters
        if [ $number_letters = 5 ]; then
                echo "Letter One"
                read letterA
                echo "Letter two"
                read letterB
                echo "Letter three"
                read letterC
                echo "Letter Four" 
                read letterD
                echo "Letter Five"
                read letterE
                grep -Ei "([$letterA])" ./wordleguesses2.txt | grep -Ei "([$letterB])" | grep -Ei "([$letterC])" | grep -Ei "([$letterD])" | grep -Ei "([$letterE])" > ./wordleguesses.txt
                elif [ $number_letters = 4 ]; then
                        echo "Letter One"
                        read letterA
                        echo "Letter Two"
                        read letterB
                        echo "Letter Three"
                        read letterC
                        echo "Letter Four"
                        read letterD
                        grep -Ei "([$letterA])" ./wordleguesses2.txt | grep -Ei "([$letterB])" | grep -Ei "([$letterC])" | grep -Ei "([$letterD])" > wordleguesses.txt
elif [ $number_letters = 3 ]; then 
                                echo "Letter One" 
                                read letterA
                                echo "Letter Two"
                                read letterB
                                echo "Letter Three"
                                read letterC
                                grep -Ei "([$letterA])" wordleguesses2.txt | grep -Ei "([$letterB])" | grep -Ei "([$letterC])" > wordleguesses.txt
                                elif [ $number_letters = 2 ]; then 
                                        echo "Letter One"
                                        read letterA
                                        echo "Letter Two" 
                                        read letterB
                                        grep -Ei "([$letterA])" wordleguesses2.txt | grep -Ei "([$letterB])" > wordleguesses.txt
                                        elif [ $number_letters = 1 ]; then
                                                echo "Letter One"
                                                read letterA
                                                grep -Ei "([$letterA])" wordleguesses2.txt > wordleguesses.txt

        fi
        elif [ $answer == N ]; then
        cat wordleguesses2.txt > wordleguesses.txt  
        #fi
fi
echo "Your Wordle suggestions are:"
echo "============================================================="
cat wordleguesses.txt
echo "============================================================="
rm wordleg*

The script is pretty simple with what it does. It asks me what letter range I want to use for each letter then puts that in grep. It then asks if I want to eliminate letter and then specify a word must have specific letters. All that gets put into a text file and then read out. You can see a video of it in action below.

Building this script too longer than I thought it would. At first, I thought it would be a simple matter of accepting inputs for a variable and then plugging that variable into grep, and indeed that’s most of the script. However, I quickly realized that this was going to get more complicated as I needed a way to plug in more than one letter when I knew more than one existed but didn’t know where.

I’m sure this is where a more experienced programmer could improve on my code by not having the script run through an If-this-than-that loop, but for now; it works fine.

Future improvements

One issue I came across when testing this script on Octordles is that I’m still typing a lot of letters as I try to eliminate letters. I’m probably going to work on assigning these as a variable and just adding to it. I’ll post the new code when I get it finished.

For now, this has been a fun improvement on my previous post.

WordPress Appliance - Powered by TurnKey Linux