Part 2 on day 5

This commit is contained in:
pingu 2024-12-05 09:58:39 +01:00
parent 3b2cad32ab
commit 1d7c8f9996

View File

@ -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)