## Haskell - 173

Instead of searching directly on the grid, I transform the grid in different ways and match the word with each row of the new grid.

For example, 
    
    G1    G2    G3       G4   G5
   
    abcd  aA1   abcd     a..  ..1
    ABCD  bB2   .ABCD    bA.  .A2
    1234  cC3   ..1234   cB1  aB3
          dD4            dC2  bC4
                          D3  cD
                           4  d
Search the word in each row of G1, G2, G4 and G5, then we're done. Note that G3 is not used, I post it here just for illustration.

A similar idea is applied to search forward and backward: just search the original word and reversed word.

So now we have searched 8 directions. Here's the code, whose correctness was verified by [another script][1].

    import Data.List
    v=reverse
    t=transpose
    y=any
    d r=zipWith(++)(scanr(\_->('\n':))[]r)r
    g r w=y(y$y((==w).take(length w)).tails)[r,t r,t.d$r,t.d.v$r]
    f r w=y(g(lines r))[w,v w]

The function `f` is what we want and its argument `r` is the rectangle string, `w` is the word to search.

  [1]: http://pastebin.com/KsG1QTnn