summaryrefslogtreecommitdiff
path: root/5-1.hs
diff options
context:
space:
mode:
authorLaura Orvokki Kursula <lav@vampires.gay>2024-12-05 09:54:16 +0100
committerLaura Orvokki Kursula <lav@vampires.gay>2024-12-05 09:58:17 +0100
commitb874331adf794d9b3b0683111ed3ceba1f87efb6 (patch)
tree217d87d3f27e1927c3b25363d9a9d24ee540eba8 /5-1.hs
parentf7b5d3a1d7fc509e105b9b58a6519fe3d50a91a8 (diff)
downloadaoc2024-b874331adf794d9b3b0683111ed3ceba1f87efb6.tar.gz
aoc2024-b874331adf794d9b3b0683111ed3ceba1f87efb6.zip
5-1
Diffstat (limited to '5-1.hs')
-rw-r--r--5-1.hs30
1 files changed, 30 insertions, 0 deletions
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