diff --git a/app/5.hs b/app/5.hs index b1c6038..b4252db 100644 --- a/app/5.hs +++ b/app/5.hs @@ -2,6 +2,7 @@ module Main where import Data.Functor +import Data.List import Data.List.Split import Data.Map (Map) import qualified Data.Map as Map @@ -11,17 +12,24 @@ parse s = let [a,b] = splitOn "\n\n" s in ( foldr (\[x,y] m -> Map.insertWith (++) (read x) ([read y]) m) Map.empty . (splitOn "|" <$>) $ lines a , ((read <$>) . splitOn "," <$>) $ lines b) -fix1 :: Map Int [Int] -> [Int] -> [Int] -fix1 _ [ ] = [ ] -fix1 _ [x] = [x] -fix1 r (x:y:es) = undefined --TODO + +inOrder :: Map Int [Int] -> [Int] -> [Int] -> Bool +inOrder _ _ [] = True +inOrder r p (x:xs) = (case Map.lookup x r of + Just l -> (== []) $ p `intersect` l + Nothing -> True) && inOrder r (x:p) xs solve1 :: Map Int [Int] -> [[Int]] -> Int -solve1 r l = sum $ l <&> (\a -> let t = length a in a !! (t `div` 2)) . fix1 r +solve1 r l = sum $ filter (inOrder r []) l <&> (\a -> let t = length a in a !! (t `div` 2)) -solve2 = undefined +sort' :: Map Int [Int] -> [Int] -> [Int] -> [Int] +sort' _ p [] = p +sort' r p (x:xs) = undefined + +solve2 :: Map Int [Int] -> [[Int]] -> Int +solve2 r l = sum $ l <&> (\a -> let t = length a in a !! (t `div` 2)) . sort' r [] main :: IO () -main = readFile "inputs/5.example" <&> parse >>= \i -> +main = readFile "inputs/5" <&> parse >>= \i -> print (uncurry solve1 i) --print (solve2 i)