5

I need to solve a puzzle using shell script. I tried to combine grep with rev and saved the output into a temporary text file but still don't know how to solve it entirely.

That's the puzzle to solve :

j s e t f l
a l s f e l
g a a n p l
e p f d p k
r e g e l a
f n e t e n

The file that contains the wordlist to use is in http://pastebin.com/DP4mFZAr

I know how to tell grep where to find the patterns to match as fixed strings extracted from a text file using $ grep -Ff wordlist puzzle and how to search for mirrored words using $ rev puzzle | grep -Ff wordlist puzzle , thus dealing with the horizontal lines, but how do I deal with vertical words too ?

jihed gasmi
  • 279
  • 4
  • 9

1 Answers1

18

I am covering horizontal and vertical matching. The main idea is to remove the spaces and then use grep -f with the given list of words, stored in words file.

With grep -f, the results are shown within the line. If you just want to see the matched test, use grep -of.

Horizontal matching

$ cat puzzle | tr -d ' ' | grep -f words
alsfel
gaanpl
regela
fneten

$ cat puzzle | tr -d ' ' | grep -of words
als
gaan
regel
eten

Vertical matching

For this, we firstly have to transpose the content of the file. For this, I use what I used for another answer of mine:

transpose () {
  awk '{for (i=1; i<=NF; i++) a[i,NR]=$i; max=(max<NF?NF:max)}
        END {for (i=1; i<=max; i++)
              {for (j=1; j<=NR; j++) 
                  printf "%s%s", a[i,j], (j<NR?OFS:ORS)
              }
        }'
}

And let's see:

$ cat puzzle | transpose | tr -d ' ' | grep -f words
jagerf
slapen
esafge
tfndet
lllkan

$ cat puzzle | transpose | tr -d ' ' | grep -of words
jager
slapen
af
ge
de
kan

You can then use rev (as you suggest in your question) for mirrored words. Also tac can be interesting for vertically mirrored words.

Diagonal matching

For the diagonal matching, I think that an interesting approach would be to move every single line a little bit to the left/right. This way,

e x x x x
x g x x x
x x g x x

can become

e x x x x
g x x x
g x x

and you can use the vertical/horizontal approaches.

For this, you can use printf as described in Using variables in printf format:

$ cat a
e x x x x
x g x x x
x x g x x
$ awk -v c=20 '{printf "%*s\n", c, $0; c-=2}' a
        e x x x x
        x g x x x
    x x g x x
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 10
    Dude I love you. Do you have Paypal? I'd like to buy you a beer. –  Oct 22 '14 at 15:07
  • 2
    Haha you are welcome. Yes, I would ask for a La Chouffe :D I added some explanation about how to match diagonally. – fedorqui Oct 22 '14 at 15:22
  • 2
    PM me your Paypal and I'll get you money for a La Chouffe! –  Oct 22 '14 at 15:36
  • 3
    Absolutely off-topic here, but man! you have great tastes! La Chouffe is one of my favorite beers too! – gniourf_gniourf Oct 22 '14 at 15:40
  • 1
    You guys should try 'Kasteel Cuvée du Château'. Its quite wonderful (also not for the faint of heart- its 11,6%). –  Oct 22 '14 at 16:15
  • @Menubalk uhms, will keep that one in memory. This is the best present you can give me: a set of names of beers to try! – fedorqui Oct 22 '14 at 17:44
  • Kasteel Donker is another beer of similiar strength. You can also try Kwak bier and La Trappe Tripel :). Also, I made a shell script out of your solution, but for some reason it doesn't work.. here it is: http://pastebin.com/3K0S23qG –  Oct 22 '14 at 20:09
  • 1
    I fixed it. Thanks for all your help anyway! and enjoy the beers, haha –  Oct 22 '14 at 20:29