summaryrefslogtreecommitdiff
path: root/4-2.hs
blob: 9a5c327298cb4aa0c333ee80a52bf3819b960d78 (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
import Control.Monad (ap)

width, height :: [[a]] -> Int
height = length
width  = length . head

crop :: Int -> Int -> [[a]] -> [[a]]
crop x y = map (take 3 . drop x) . take 3 . drop y

diag :: [[a]] -> [a]
diag xss = [ xss !! x !! x | x <- [0 .. length xss - 1] ]

diags :: ([a] -> Bool) -> [[a]] -> Bool
diags p = all p . ((diag .) <$> [ id, map reverse ] <*>) . pure

mas :: String -> Bool
mas = elem "MAS" . ap [ id, reverse ] . pure

count :: [[Char]] -> Int
count xss = length . filter id $ [ diags mas (crop x y xss)
                                 | x <- [0 .. width xss - 3]
                                 , y <- [0 .. height xss - 3]
                                 ]

main :: IO ()
main = getContents >>= print . count . lines