From 175760995026cc04c5b41e199bc32f445de37aac Mon Sep 17 00:00:00 2001 From: Xiretza Date: Thu, 3 Dec 2020 22:33:03 +0100 Subject: [PATCH] day3: add haskell solution --- day3/day3.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 day3/day3.hs diff --git a/day3/day3.hs b/day3/day3.hs new file mode 100644 index 0000000..5d9cb30 --- /dev/null +++ b/day3/day3.hs @@ -0,0 +1,23 @@ +import Control.Applicative (empty) +import Control.Monad.Zip (mzip) +import Data.List (intercalate) +import Data.Maybe (catMaybes) + +compose2 :: (a' -> b' -> c) -> (a -> a') -> (b -> b') -> a -> b -> c +compose2 f g h x y = f (g x) (h y) + +(.:) :: (c -> d) -> (a -> b -> c) -> a -> b -> d +f .: g = (f .) . g +infixl 8 .: + +countHits :: (Int, Int) -> [[Bool]] -> Int +countHits = length . filter id . map (uncurry $ flip (!!)) . catMaybes .: compose2 (zipWith mzip) (uncurry . flip $ compose2 intercalate (flip replicate empty . pred) (fmap pure . map pure . enumFromThen 0)) (map pure) + +treeCharToBool :: Char -> Bool +treeCharToBool = (== '#') + +main = do + charLines <- lines <$> readFile "input.txt" + let boolLines = cycle <$> map treeCharToBool <$> charLines + print $ countHits (3, 1) boolLines + print $ product $ flip countHits boolLines <$> [(1,1), (3,1), (5,1), (7,1), (1,2)]