1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import Data.List (tails, transpose)
main :: IO ()
main = do
let a `startsWith` b = take (length b) a == b
let searchIn word lns = length (filter (`startsWith` word) (tails lns)) + length (filter (`startsWith` word) (tails (reverse lns)))
let rows = id
let cols = transpose
let unshift = zipWith drop [0..]
let maindiag = concatMap (take 1) . unshift
let upperdiags = transpose . unshift
let diags1 mat = map maindiag (tails (tail mat)) ++ upperdiags mat
let diags2 = diags1 . map reverse
input <- lines <$> getContents
print $ sum (map (searchIn "XMAS") (concatMap ($ input) [rows, cols, diags1, diags2]))
let chunks n = filter ((== n) . length) . map (take n) . tails
let blocks n = concatMap (transpose . map (chunks n)) . chunks n
let rots blk = [blk, map reverse (transpose blk), reverse (map reverse blk), reverse (transpose blk)]
let isx_mas [[a,_,b],[_,c,_],[d,_,e]] = a == 'M' && b == 'M' && c == 'A' && d == 'S' && e == 'S'
print $ sum [fromEnum (isx_mas blk) | blk <- concatMap rots (blocks 3 input)]
|