aoc/app/9.hs
2024-12-09 10:26:30 +01:00

38 lines
913 B
Haskell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{-# LANGUAGE LambdaCase #-}
module Main where
import Data.Functor
import Data.List
import Data.List.Extra
data where
None ::
Num :: Int ->
deriving Eq
instance Show where
show None = show '.'
show (Num n) = show n
parse :: String -> []
parse s = zip [0..] (trim s) >>= \(i,a) -> replicate (read [a]) (if even i then Num (i `div` 2) else None)
sort1 :: [] -> []
sort1 s
| None `notElem` s = s
| otherwise = case last s of
None -> sort1 $ init s
n -> sort1 $ takeWhile (/= None) s ++ [n] ++ drop 1 (dropWhile (/= None) (init s))
sort2 :: [] -> []
sort2 = undefined
solve :: ([] -> []) -> [] -> Int
solve f s = let fixed = (\(Num n) -> n) <$> f s in
sum $ zipWith (*) fixed [0..]
main :: IO ()
main = readFile "inputs/9.example" <&> parse >>= \i ->
print (solve sort1 i) >>
print (solve sort2 i)