From b874331adf794d9b3b0683111ed3ceba1f87efb6 Mon Sep 17 00:00:00 2001 From: Laura Orvokki Kursula Date: Thu, 5 Dec 2024 09:54:16 +0100 Subject: 5-1 --- 5-1.hs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 5-1.hs (limited to '5-1.hs') diff --git a/5-1.hs b/5-1.hs new file mode 100644 index 0000000..c84d1f6 --- /dev/null +++ b/5-1.hs @@ -0,0 +1,30 @@ +pairs' :: [a] -> [(a,a)] -> [(a,a)] +pairs' [] ys = ys +pairs' (x:xs) ys = pairs' xs (ys ++ map (x,) xs) + +pairs :: [a] -> [(a,a)] +pairs xs = pairs' xs [] + +ordered :: Eq a => [(a,a)] -> [a] -> Bool +ordered ps xs = all (`elem` pairs xs) $ filter f ps + where + f (p,q) = p `elem` xs && q `elem` xs + +parse :: String -> ([(Int,Int)], [[Int]]) +parse s = (map pair ls, map list ks) + where + (ls,_:ks) = span (/= "") (lines s) + pair r = let (p,_:q) = span (/= '|') r in (read p, read q) + list r = read ("[" ++ r ++ "]") :: [Int] + +corrects :: String -> [[Int]] +corrects s = let (ps,xss) = parse s in filter (ordered ps) xss + +middle :: [a] -> a +middle xs = xs !! (length xs `div` 2) + +doThing :: String -> Int +doThing = sum . map middle . corrects + +main :: IO () +main = getContents >>= print . doThing -- cgit v1.2.3