diff --git a/app/9.hs b/app/9.hs index 17221a2..3b3da21 100644 --- a/app/9.hs +++ b/app/9.hs @@ -3,7 +3,9 @@ module Main where import Data.Functor import Data.List -import Data.List.Extra +import Data.List.Split +import Data.List.Extra hiding (split) +import Debug.Trace data ℜ where None :: ℜ @@ -24,11 +26,25 @@ sort1 s None -> sort1 $ init s n -> sort1 $ takeWhile (/= None) s ++ [n] ++ drop 1 (dropWhile (/= None) (init s)) +chunks :: [ℜ] -> [[ℜ]] +chunks = + init . foldr (\a (x:xs) -> if a `elem` x then (a:x):xs else [a]:x:xs) [[]] + +fit :: [[ℜ]] -> [ℜ] -> [ℜ] +fit s t = let (p,a) = break (elem None) s + (n,a') = span (elem None) a in + if null n then concat s ++ t else + concat p ++ if length n >= length t then t ++ concat a' ++ replicate (length n) None else fit a' t + sort2 :: [ℜ] -> [ℜ] -sort2 = undefined +sort2 s = let c = chunks s -- TODO: needs more work here + fitted = fit (init c) (last c) in + if s == fitted then s else sort2 s solve :: ([ℜ] -> [ℜ]) -> [ℜ] -> Int -solve f s = let fixed = (\(Num n) -> n) <$> f s in +solve f s = let fixed = (\case + Num n -> n + None -> 0) <$> f s in sum $ zipWith (*) fixed [0..] main :: IO ()