blob: 7fa191a1b3660a619f2ab853843def45d47445d8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import Control.Monad (ap)
transpose :: [[a]] -> [[a]]
transpose ([]:_) = []
transpose xss = map head xss : transpose (map tail xss)
diagonals :: Eq a => [[a]] -> [[a]]
diagonals a =
let n = length a
m = length (head a)
in
[[ a!!x!!(d-x) | x <- [max 0 (d-m+1) .. min (n-1) d] ]
| d <- [0..n+m-1]]
reverseRows :: [[a]] -> [[a]]
reverseRows = map reverse
ts :: Eq a => [[[a]] -> [[a]]]
ts = [ id
, reverseRows
, transpose
, reverseRows . transpose
, diagonals
, reverseRows . diagonals
, diagonals . reverseRows
, reverseRows . diagonals . reverseRows
]
transforms :: Eq a => [[a]] -> [[[a]]]
transforms = ap ts . pure
count :: Eq a => [a] -> [a] -> Integer
count = count' 0
count' :: Eq a => Integer -> [a] -> [a] -> Integer
count' n _ [] = n
count' n s xs = if take (length s) xs == s
then count' (n+1) s (drop (length s) xs)
else count' n s (tail xs)
result :: [String] -> Integer
result = sum . map (sum . map (count "XMAS")) . transforms
main :: IO ()
main = getContents >>= print . result . lines
|