How to cheat on Wordle using Linux


Wordle—the online game that gives you six tries to guess a five-letter word—has gone viral recently, and while it’s fun, it can also be pretty hard. So, as a bash-scripting enthusiast, I figured I’d see if I could come up with a script that would help me cheat.

The game itself is fairly simple. After you enter a five-letter guess, the game indicates which of its letters are not in the mystery word by setting them off on a gray background, which ones are in the word but in the wrong location (orange background), and which ones are in the word and located in the right place (green background). Each guess must be a known English word, no capitals, no punctuation.

In the example below, each guess includes some letters not yet tried and also moves the position of the A in hopes of finding out where it belongs.

wordle 1 Sandra Henry-Stocker

The strategy behind the script

Before presenting the script, which I call “cheat” for obvious reasons, let me explain how it works. After all, more than encouraging people to cheat, I’d like to encourage them to write bash scripts. So, here goes.

First, cheat creates a file that includes all of the five-letter words in the system’s words file (e.g., /usr/share/dict/words). It ensures that words like “Tufts” and “tufts” are not both included by restricting the word list to lowercase letters and removing what might otherwise be duplicates. It also ignores hyphenated words like “yo-yo” since Wordle doesn’t deal with hyphens any more than it differentiates upper- and lowercase characters. The script also drops any words that contain periods (like the abbreviation “zool.“) as Wordle doesn’t tolerate those either.

It next asks the individual running the script to supply the letters they know are correct and in the right positions. Cheat requires that the individual knows at least one, so this step takes place after the person has made two or three guesses and knows some of the letters.

In the example below, the person has made four guesses and only has two remaining. Notice that uppercase characters are required for this script, so you might want to hit that CAPS key before you start. In this case, two letters are in the right locations and one is not.

wordle 2 Sandra Henry-Stocker

When they get to “spite”, they decide they have enough information about the letters to run the cheat script. Here’s what they see:

$ ./cheat
enter known letters in '.C.DS' format: SP...
enter misplaced letters in blank-separated format: F
supply blank-separated list of unusable letters: Y R I C L A E T
SPOOF

The end result of running the cheat script was the word “SPOOF”, which turned out to be correct.

Note that the person running the script supplied the two letters that were verified (“S”and “P”), the one letter that was reported as being in the wrong position (“F”) and the letters that weren’t included in the word (“Y”, “R”, “I”, “C”, “L”, “A”, “E”, and “T”). The person did not include the second “S” in STEPS as being not included because this would have blocked the letter “S” altogether. The fact that the second “S” has a gray background tells us there is only one “S” in the word and the second one is not it. If there were a second “S” but in the wrong position, it would have an orange background.

In the separate example below, cheat returned five matches, and two look promising (inept and inert).

$ ./cheat
enter known letters in '.C.DS' format: i.e..
enter misplaced letters in blank-separated format: t
supply blank-separated list of unusable letters: a d u s m
ibert
inept
inert
ivett

The “.C.DS” format is used because grep understands that this means the first and third characters can match any character while the other positions must match the letters provided.

The cheat script

The cheat script is displayed below and includes a fair share of comments to explain itself. It generates a list of five-letter words each time it’s run. This may seem like a little overkill, but it ensures the file is available and takes under one second.

The script then prompts the user for the information as shown in the example above and uses the letters provided to generate sequences of grep commands that ensure that certain letters are excluded (grep -v commands) from the solutions list and others are included (grep without the -v).

#/bin/bash
 
# generate list of 5-letter words
grep ^.....$ /usr/share/dict/words | tr '[:upper:]' '[:lower:]' 
   | grep -v "-" | grep -v "." | uniq > 5letters
# get letters in known positions (green)
echo -n "enter known letters in '.c.ds' format: "
read known
# get letters in wrong positions (orange)
echo -n "enter misplaced letters in blank-separated format: "
read misplaced
# get letters known to be not included in the word (gray)
echo -n "supply blank-separated list of unusable letters: "
read letters
exclude=""
include=""
# create grep command with list of letters for grep to exclude
for L in $letters
do
  exclude="$exclude | grep -v '$L' "
done
# create grep command with list of letters for grep to require
for L in $misplaced
do
  include="$include | grep '$L' "
done
# remove initial "|"
exclude=`echo $exclude | cut -c2-`
include=`echo $include | cut -c2-`
# create list of matches
if [ ${#include} != 0 ]; then
  grep $known 5letters | eval $exclude | eval $include
else
  grep $known 5letters | eval $exclude
fi

The end result should be a list of words that fit the requirements. The more letters you can provide, the shorter the list will be. In the first example shown above, the word “SPOOF” was the only word listed, but you could end up with quite a few choices. Nailing down a few letters before you resort to cheating will generally give you the best chance at finding the correct word.

Wrap Up

Playing Wordle might be fun, but so is writing bash scripts! Feel free to use, modify or improve the cheat script included in this post.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Copyright © 2022 IDG Communications, Inc.



Source link