diff options
author | Laura Orvokki Kursula <lav@vampires.gay> | 2024-12-05 09:54:16 +0100 |
---|---|---|
committer | Laura Orvokki Kursula <lav@vampires.gay> | 2024-12-05 09:58:17 +0100 |
commit | b874331adf794d9b3b0683111ed3ceba1f87efb6 (patch) | |
tree | 217d87d3f27e1927c3b25363d9a9d24ee540eba8 /5-1.hs | |
parent | f7b5d3a1d7fc509e105b9b58a6519fe3d50a91a8 (diff) | |
download | aoc2024-b874331adf794d9b3b0683111ed3ceba1f87efb6.tar.gz aoc2024-b874331adf794d9b3b0683111ed3ceba1f87efb6.zip |
5-1
Diffstat (limited to '5-1.hs')
-rw-r--r-- | 5-1.hs | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -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 |