Cheating at Spelling Bee using the command line


The New York Times Spelling Bee is a free online word game, where players must construct as many words as possible using the letters provided in the Bee’s grid, always including the middle letter. Bonus points for using all the letters and creating a pangram.


However, this is the kind of thing which computers are very good at. If you’ve become frustrated trying to decipher the abstruse ways of the bee, let the command line help.

tl;dr:

grep -iP "^[adlokec]{6,}$" /usr/share/dict/words |grep a | awk '{ print length, $0 }' |sort -n |cut -f2 -d" "

Starting with the grid, put each of the letters into grep:

grep -iP "^[adlokec]{6,}$" /usr/share/dict/words 

This will find all words in /usr/share/dict/words, which contain the letters a,d,l,o,k,e or c.
-i is case-insensitive,
-P is use Perl regexp
^ is the start of the word
$ is the end of the word
{6,} is repeat the previous search six or more times

This will give us a list of words, however the bee requires us to find a word that also contains the centre letter, so we need to refine the results with an extra grep:

grep -iP "^[adlokec]{6,}$" /usr/share/dict/words |grep a

Now we’ve got some useful results!

Callao
accede
acceded
accolade
addled
cackle

However, they’d be more useful if they were in order of length, since we get more points for longer words.


Now comes the fiddly bit, it’s given us useful answers, but we want the longest words with the most points. That’s where awk comes in. By passing our results to awk, we can get awk to prefix the length of the word, ready for sorting.

grep -iP "^[adlokec]{6,}$" /usr/share/dict/words | grep a | awk '{ print length, $0 }' 

6 Callao
6 accede
7 acceded
8 accolade
6 addled

Now, as before, pipe the results to the next stage in the pipeline.. In this case, sort.

grep -iP "^[adlokec]{6,}$" /usr/share/dict/words | grep a | awk '{ print length, $0 }' | sort -n

sort gives us:
-n, –numeric-sort
compare according to string numerical value

Our output is now sorted by length and looks like:

7 cloaked
7 cockade
8 accolade
8 deadlock
10 deadlocked

Finally, to leave us with a clean, sorted list and appease the Bee, we want to remove the numbers which awk put in. So finally we pipe our output to cut.

grep -iP "^[adlokec]{6,}$" /usr/share/dict/words | grep a | awk '{ print length, $0 }' | sort -n | cut -f2 -d" "

-d” ” use a space (” “) instead of TAB as a field delimiter
-f 2 return the second field

Callao
accede
addled
cackle
called
coaled
decade
doodad
lacked
ladled
leaded
leaked
loaded
locale
acceded
cackled
clacked
cloaked
cockade
accolade
deadlock
deadlocked

Author