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

60 lines
1.8 KiB
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.Split
import Data.List.Extra hiding (split)
import Debug.Trace
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))
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 (head n) >= length t then t ++ replicate (length n) None ++ concat a' else concat n ++ fit a' t
sort2 :: [] -> []
sort2 s = let c = chunks s in
if notElem None $ last c
then let fitted = fit (init c) (last c) in
if s == fitted then head c ++ case c of
[] -> []
[c] -> c
c@[_,_] -> concat c
c -> sort2 (concat . tail $ init c) ++ last c else sort2 fitted
else sort2 (concat $ init c) ++ last c
solve :: ([] -> []) -> [] -> Int
solve f s = let fixed = (\case
Num n -> n
None -> 0) <$> f s in
sum $ zipWith (*) fixed [0..]
main :: IO ()
main = readFile "inputs/9.example" <&> parse >>= \i ->
print (solve sort1 i) >>
print (solve sort2 i)