summaryrefslogtreecommitdiff
path: root/4-2.hs
diff options
context:
space:
mode:
Diffstat (limited to '4-2.hs')
-rw-r--r--4-2.hs26
1 files changed, 26 insertions, 0 deletions
diff --git a/4-2.hs b/4-2.hs
new file mode 100644
index 0000000..9a5c327
--- /dev/null
+++ b/4-2.hs
@@ -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