Solving worldle, the unix way!

· Anand's blog


This post is a draft. It is work in progress.

On Unix computers, the file /usr/share/dict/words contains all the words in dictionary.

$ head /usr/share/dict/words
A
AA
AAA
AA's
AB
ABC
ABC's
ABCs
ABM
ABM's

Those don't look like real words. Let's shuffle the words and see 10 of them.

$ cat /usr/share/dict/words | shuf | head
orienting
bacilli
scummy
intones
belladonna
Ellesmere's
newlyweds
sustain
rattlings
military

Now it looks like real words.

Let's see how many of them are there.

$ cat /usr/share/dict/words | wc -l
104334

That is quite a lot. A hundred thousand of them!

However, we are only interested in 5 letter words.

$ cat /usr/share/dict/words | grep '^.....$' | head
ABC's
ABM's
AFAIK
AFC's
AMD's
ANSIs
ANZUS
AOL's
ASCII
ASL's

Let's shuffle them and see.

$ cat /usr/share/dict/words | grep '^.....$' | shuf | head
mutes
water
Golda
talon
viced
jeeps
rills
dolly
haler
hikes

That looks interesting. Let's see how many such words are there.

$ cat /usr/share/dict/words | grep '^.....$' | wc -l
7044

About seven thousand. That is not bad.

There are a couple of issues here. There are some words with single quote or apostrophe. We are not interested in them.

Also, some of the words have some letters in uppercase. Mosy unix commands being case sensitive, that may give us some trouble down the line.

It is not apparent in the examples above, but there are some duplicate words, with case variations. We need to fiter them out too.

Let's address both these issues.

$ cat /usr/share/dict/words | grep '^.....$' | grep -v "'" | head
AFAIK
ANSIs
ANZUS
ASCII
ASPCA
AWACS
Aaron
Abbas
Abdul
Abner

And let's convert all the words into lower case.

$ cat /usr/share/dict/words | grep '^.....$' | grep -v "'" | tr 'A-Z' 'a-z' |  head
afaik
ansis
anzus
ascii
aspca
awacs
aaron
abbas
abdul
abner

And the number of words could have come down.

$ cat /usr/share/dict/words | grep '^.....$' | grep -v "'" | tr 'A-Z' 'a-z' |  wc -l
6240

Let's remove the duplicates as well.

$ cat /usr/share/dict/words | grep '^.....$' | grep -v "'" | tr 'A-Z' 'a-z' |  sort -u | wc -l
6030

I'm going to create an alias five-letter-words instead of typing the whole command again and again. Please note that the double quotes in the command are escaped.

$ alias five-letter-words="cat /usr/share/dict/words | grep '^.....$' | grep -v \"'\" | tr 'A-Z' 'a-z' | sort -u"

$ five-letter-words | head
aaron
abaci
aback
abaft
abase
abash
abate
abbas
abbés
abbey

$ five-letter-words | wc -l
6030

Solving Worldle #

Let's pick a random word to start with.

$ five-letter-words | shuf | head
haber
nears
jenny
beaus
emend
evict
chile
iraqi
algae
kneed

I'll pick evict.

The letters v and i are present in the word, but not in the right place. The letters e, c and t are not present.

$ five-letter-words | grep -v '[ect]' | grep v | grep i | grep '.[^v][^i]..' | wc -l
44

That just leaves 44 words.

$ five-letter-words | grep -v '[ect]' | grep v | grep i | grep '.[^v][^i]..'  | shuf | head
mavin
davis
invar
livia
xxxvi
livid
virgo
vizor
vidal
villa

Let's pick a random words out of them. I'll pick livid.

$ five-letter-words | grep -v '[ectd]' | grep v | grep i | grep l | grep '[^l][^v][^iv][^i].'
silva
vials
villa
vilma
vinyl
viola
viols
viral

We are down to just 8 words. I'll pick viral this time.

$ five-letter-words | grep -v '[ectdr]' | grep v | grep i | grep l | grep 'vi[^iv][^ia][^l]'
vials
villa
vilma
viola
viols

Let me try with the first match vials.

$ five-letter-words | grep -v '[ectdrs]' | grep v | grep i | grep a | grep l | grep 'vi[^iva]l[^l]'
villa
viola

There are only 2 words left. Let me try the first one villa.

Yay! we've done it!!

last updated: