Miskatonic University Press

Double-grepping to solve a crossword puzzle

puzzles unix

Puzzlebomb is a monthly puzzle page made by Katie Steckles and Paul Taylor. I recently subscribed and am enjoying having the sheet up on my fridge so I can work away at it.

There’s a “G _ P W _ R D” puzzle in the current issue: a crossword where some of the letters in the clues are missing, and you learn what they are by filling in the grid. As they put it, “Solve the clues to determine letters to write in the grids to fill in the gaps in the clues in order to put the answers in.”

It may be hard to understand it all without showing the grids, but here’s how I solved one (two) clues that had me a bit stuck. Once I thought about this way of solving I had to try it.

The clue is a word with a missing first letter:

x I T C H

And I know from the grid that the first letter of the clue is the first letter of the answer:

x _ A _ N

There are just a few options for the first word, but many more for the second. This shell script goes through all the words in /usr/share/dict/words, finds the ones that match the first pattern, picks out their first letters, then finds any words that match the second pattern.

#!/bin/bash

dict="/usr/share/dict/words"

grep -E -i "^..a.n$" $dict | while read -r word
do
    init=${word:0:1}
    grep -E -i "^${init}itch$" $dict | while read -r clue
    do
     	echo "Does $clue mean $word?"
    done
done

With the options to grep, "^..a.n$" means “do a case-insensitive search for any word that is exactly five letters long where the third letter is A and the fifth is N.”

Does bitch mean Brain?
Does ditch mean Deann?
Does ditch mean Diann?
Does bitch mean brain?
Does bitch mean brawn?
Does ditch mean drain?
Does ditch mean drawn?
Does pitch mean plain?
Does pitch mean prawn?

There we are: DITCH and DRAIN. Solved!

(I used to do for word in $(grep foo $dict), but shellcop told me to do it with while read, so I’m trying that out.)