summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaura Orvokki Kursula <lav@vampires.gay>2024-12-04 23:07:38 +0100
committerLaura Orvokki Kursula <lav@vampires.gay>2024-12-04 23:23:45 +0100
commitf7b5d3a1d7fc509e105b9b58a6519fe3d50a91a8 (patch)
tree765634636d6cecd88ed5cb48d295187468770b35
parent3a15eef7459c586b0dcb8138d1f2c3f0ff1da64f (diff)
downloadaoc2024-f7b5d3a1d7fc509e105b9b58a6519fe3d50a91a8.tar.gz
aoc2024-f7b5d3a1d7fc509e105b9b58a6519fe3d50a91a8.zip
4-2
-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