aoc/app/9.hs

38 lines
913 B
Haskell
Raw Normal View History

2024-12-02 10:51:49 +00:00
{-# LANGUAGE LambdaCase #-}
module Main where
import Data.Functor
2024-12-09 09:26:30 +00:00
import Data.List
import Data.List.Extra
2024-12-02 10:51:49 +00:00
2024-12-09 09:26:30 +00:00
data where
None ::
Num :: Int ->
deriving Eq
2024-12-02 10:51:49 +00:00
2024-12-09 09:26:30 +00:00
instance Show where
show None = show '.'
show (Num n) = show n
2024-12-02 10:51:49 +00:00
2024-12-09 09:26:30 +00:00
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..]
2024-12-02 10:51:49 +00:00
main :: IO ()
2024-12-09 09:26:30 +00:00
main = readFile "inputs/9.example" <&> parse >>= \i ->
print (solve sort1 i) >>
print (solve sort2 i)