diff options
-rw-r--r-- | 4-2.hs | 26 |
1 files changed, 26 insertions, 0 deletions
@@ -0,0 +1,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 |