Solve the puzzle using bash tools like grep

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

To solve the puzzle:

jsetfl alsfel gaanpl epfdpk regela fneten 

A file containing a list of words to use is located at http://pastebin.com/DP4mFZAr

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

+5
source share
1 answer

I cover horizontal and vertical match. The basic idea is to remove the spaces, and then use grep -f with the given list of words stored in the words file.

With grep -f results are displayed in a string. If you just want to see a consistent test, use grep -of .

Horizontal mapping

 $ cat puzzle | tr -d ' ' | grep -f words alsfel gaanpl regela fneten $ cat puzzle | tr -d ' ' | grep -of words als gaan regel eten 

Vertical mapping

To do this, you first need to transfer the contents of the file. For this, I use what I used for my other answer :

 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 

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

Diagonal matching

For diagonal comparisons, I think an interesting approach would be to move each individual line a bit left / right. Thus,

 exxxx xgxxx xxgxx 

can be

 exxxx gxxx gxx 

and you can use vertical / horizontal approaches.

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

 $ cat a exxxx xgxxx xxgxx $ awk -vc=20 '{printf "%*s\n", c, $0; c-=2}' a exxxx xgxxx xxgxx 
+18
source

Source: https://habr.com/ru/post/1205276/


All Articles