From 9c4275d2181f72aa672e3c8cbe2336eb34c19a25 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 13 Mar 2023 21:36:59 +0100 Subject: 2020-06 in haskell --- 2020/06-Custom_Customs/second.hs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2020/06-Custom_Customs/second.hs (limited to '2020/06-Custom_Customs/second.hs') diff --git a/2020/06-Custom_Customs/second.hs b/2020/06-Custom_Customs/second.hs new file mode 100644 index 0000000..10d0ff4 --- /dev/null +++ b/2020/06-Custom_Customs/second.hs @@ -0,0 +1,25 @@ +-- requires cabal install --lib split Unique +module Main (main) where + +import Control.Monad (void, when) +import Data.List (intersect, foldl') +import Data.List.Split (splitOn) +import System.Exit (die) + +exampleExpectedOutput = 6 + +parseInput :: String -> IO [String] +parseInput filename = do + input <- readFile filename + return $ map (foldl' intersect ['a'..'z'] . lines) $ splitOn "\n\n" input + +compute :: [String] -> Int +compute = sum . map length + +main :: IO () +main = do + example <- parseInput "example" + let exampleOutput = compute example + when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput) + input <- parseInput "input" + print $ compute input -- cgit v1.2.3