BreakingExpress

Resolve Wordle utilizing the Linux command line

I’ve not too long ago change into a little bit obsessive about an internet phrase puzzle recreation through which you have got six makes an attempt to guess a random five-letter phrase. The phrase adjustments on daily basis, and you’ll solely play as soon as per day. After every guess, every of the letters in your guess is highlighted: grey signifies that letter doesn’t seem within the thriller phrase, yellow signifies that letter seems within the phrase however not at that place, and inexperienced means the letter seems within the phrase at that appropriate place.

Here’s how you need to use the Linux command line that will help you play guessing video games like Wordle. I used this methodology to assist me clear up the January 6 puzzle:

First attempt

Linux techniques hold a dictionary of phrases within the /usr/share/dict/phrases file. This is a really lengthy plain textual content file. My system’s phrases file has over 479,800 entries in it. The file accommodates each plain phrases and correct nouns (names, locations, and so forth).

To begin my first guess, I simply desire a listing of plain phrases which can be precisely 5 letters lengthy. To try this, I exploit this grep command:

$ grep '^[a-z][a-z][a-z][a-z][a-z]$' /usr/share/dict/phrases > myguess

The grep command makes use of common expressions to carry out searches. You can do so much with common expressions, however to assist me clear up Wordle, I solely want the fundamentals: The ^ means the beginning of a line, and the $ means the top of a line. In between, I’ve specified 5 situations of [a-z], which signifies any lowercase letter from a to z.

I also can use the wc command to see my listing of potential phrases is “only” 15,000 phrases:

$ wc -l myguess
15034 myguess

From that listing, I picked a random five-letter phrase: acres. The a was set to yellow, that means that letter exists someplace within the thriller phrase however not within the first place. The different letters are grey, so I do know they do not exist within the phrase of the day.

Second attempt

For my subsequent guess, I wish to get an inventory of all phrases that include an a, however not within the first place. My listing also needs to not embody the letters c, r, e, or s. Let’s break this down into steps:

To get an inventory of all phrases with an a, I exploit the fgrep (fastened strings grep) command. The fgrep command additionally searches for textual content like grep, however with out utilizing common expressions:

$ fgrep a myguess > myguess2

That brings my potential listing of subsequent guesses down from 15,000 phrases to six,600 phrases:

$ wc -l myguess myguess2
 15034 myguess
  6634 myguess2
 21668 whole

But that listing of phrases additionally contains the letter a within the first place, which I do not need. The recreation already indicated the letter a exists in another place. I can modify my command with grep to search for phrases containing another letter within the first place. That narrows my potential guesses to simply 5,500 phrases:

$ fgrep a myguess | grep '^[b-z]' > myguess2
$ wc -l myguess myguess2
 15034 myguess
  5566 myguess2
 20600 whole

But I do know the thriller phrase additionally doesn’t embody the letters c, r, e, or s. I can use one other grep command to omit these letters from the search:

$ fgrep a myguess | grep '^[b-z]' | grep -v '[cres]' > myguess2
$ wc -l myguess myguess2
15034 myguess
 1257 myguess2
16291 whole

The -v possibility means to invert the search, so grep will solely return the traces that don’t match the common expression [cres] or the only listing of letters c, r, e, or s. With this additional grep command, I’ve narrowed my subsequent guess significantly to just one,200 potential phrases with an a someplace however not within the first place, and that don’t include c, r, e, or s.

After viewing the listing, I made a decision to attempt the phrase balmy.

Third attempt

This time, the letters b and a have been highlighted in inexperienced, that means I’ve these letters within the appropriate place. The letter l was yellow, in order that letter exists someplace else within the phrase, however not in that place. The letters m and y are grey, so I can get rid of these from my subsequent guess.

To determine my subsequent listing of potential phrases, I can use one other set of grep instructions. I do know the phrase begins with ba, so I can start my search there:

$ grep '^ba' myguess2 > myguess3
$ wc -l myguess3
77 myguess3

That’s solely 77 phrases! I can slender that additional by on the lookout for phrases that additionally include the letter l in wherever however the third place:

$ grep '^ba[^l]' myguess2 > myguess3
$ wc -l myguess3
61 myguess3

The ^ contained in the sq. brackets [^l] means not this listing of letters, so not the letter l. That brings my listing of potential phrases to 61, not all of which include the letter l, which I can get rid of utilizing one other grep search:

$ grep '^ba[^l]' myguess2 | fgrep l > myguess3
$ wc -l myguess3
10 myguess3

Some of these phrases would possibly include the letters m and y, which aren’t in in the present day’s thriller phrase. I can take away these from my listing of guesses with yet another inverted grep search:

$ grep '^ba[^l]' myguess2 | fgrep l | grep -v '[my]' > myguess3
$ wc -l myguess3
7 myguess3

My listing of potential phrases could be very quick now, solely seven phrases!

$ cat myguess3
babul
bailo
bakal
bakli
banal
bauld
baulk

I’ll choose banal as a probable phrase for my subsequent guess, which occurred to be appropriate.

The energy of standard expressions

The Linux command line offers highly effective instruments that will help you do actual work. The grep and fgrep instructions provide nice flexibility in scanning lists of phrases. For a word-based guessing recreation, grep helped determine an inventory of 15,000 potential phrases of the day. After guessing and understanding what letters did and didn’t seem within the thriller phrase, grep and fgrep helped slender the choices to 1,200 phrases after which solely seven phrases. That’s the ability of the command line.

Exit mobile version