Part 1 done

This commit is contained in:
pingu 2024-12-05 09:51:25 +01:00
parent 1a10659e03
commit 3b2cad32ab

View File

@ -2,6 +2,7 @@
module Main where module Main where
import Data.Functor import Data.Functor
import Data.List
import Data.List.Split import Data.List.Split
import Data.Map (Map) import Data.Map (Map)
import qualified Data.Map as 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 ( foldr (\[x,y] m -> Map.insertWith (++) (read x) ([read y]) m) Map.empty . (splitOn "|" <$>) $ lines a
, ((read <$>) . splitOn "," <$>) $ lines b) , ((read <$>) . splitOn "," <$>) $ lines b)
fix1 :: Map Int [Int] -> [Int] -> [Int]
fix1 _ [ ] = [ ] inOrder :: Map Int [Int] -> [Int] -> [Int] -> Bool
fix1 _ [x] = [x] inOrder _ _ [] = True
fix1 r (x:y:es) = undefined --TODO 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 :: 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 :: IO ()
main = readFile "inputs/5.example" <&> parse >>= \i -> main = readFile "inputs/5" <&> parse >>= \i ->
print (uncurry solve1 i) print (uncurry solve1 i)
--print (solve2 i) --print (solve2 i)