From 1d7c8f99965751c5a086eb251f022e558809a040 Mon Sep 17 00:00:00 2001 From: pingu Date: Thu, 5 Dec 2024 09:58:39 +0100 Subject: [PATCH] Part 2 on day 5 --- app/5.hs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/5.hs b/app/5.hs index b4252db..9277cda 100644 --- a/app/5.hs +++ b/app/5.hs @@ -24,12 +24,14 @@ solve1 r l = sum $ filter (inOrder r []) l <&> (\a -> let t = length a in a !! ( sort' :: Map Int [Int] -> [Int] -> [Int] -> [Int] sort' _ p [] = p -sort' r p (x:xs) = undefined +sort' r p (x:xs) = case Map.lookup x r of + Nothing -> sort' r (p ++ [x]) xs + Just l -> let t = p `intersect` l in sort' r ((p \\ t) ++ [x] ++ t) xs solve2 :: Map Int [Int] -> [[Int]] -> Int -solve2 r l = sum $ l <&> (\a -> let t = length a in a !! (t `div` 2)) . sort' r [] +solve2 r l = sum $ filter (not . inOrder r []) l <&> (\a -> let t = length a in a !! (t `div` 2)) . sort' r [] main :: IO () main = readFile "inputs/5" <&> parse >>= \i -> - print (uncurry solve1 i) - --print (solve2 i) + print (uncurry solve1 i) >> + print (uncurry solve2 i)