9th day
This commit is contained in:
parent
88e595e5f9
commit
73eec14746
25
app/9.hs
25
app/9.hs
@ -2,10 +2,7 @@
|
|||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
import Data.Functor
|
import Data.Functor
|
||||||
import Data.List
|
|
||||||
import Data.List.Split
|
|
||||||
import Data.List.Extra hiding (split)
|
import Data.List.Extra hiding (split)
|
||||||
import Debug.Trace
|
|
||||||
|
|
||||||
data ℜ where
|
data ℜ where
|
||||||
None :: ℜ
|
None :: ℜ
|
||||||
@ -34,18 +31,18 @@ fit :: [[ℜ]] -> [ℜ] -> [ℜ]
|
|||||||
fit s t = let (p,a) = break (elem None) s
|
fit s t = let (p,a) = break (elem None) s
|
||||||
(n,a') = span (elem None) a in
|
(n,a') = span (elem None) a in
|
||||||
if null n then concat s ++ t else
|
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
|
concat p ++ if length (head n) >= length t then t ++ replicate (length (head n) - length t) None ++ concat a' ++ replicate (length t) None else concat n ++ fit a' t
|
||||||
|
|
||||||
sort2 :: [ℜ] -> [ℜ]
|
sort2 :: [ℜ] -> [ℜ]
|
||||||
sort2 s = let c = chunks s in
|
sort2 s = let c = chunks s
|
||||||
if notElem None $ last c
|
fitted = fit (init c) (last c)
|
||||||
then let fitted = fit (init c) (last c) in
|
c' = chunks fitted
|
||||||
if s == fitted then head c ++ case c of
|
in
|
||||||
[] -> []
|
case length c' of
|
||||||
[c] -> c
|
0 -> concat c'
|
||||||
c@[_,_] -> concat c
|
1 -> concat c'
|
||||||
c -> sort2 (concat . tail $ init c) ++ last c else sort2 fitted
|
2 -> concat c'
|
||||||
else sort2 (concat $ init c) ++ last c
|
_ -> (sort2 . concat $ init c') ++ last c'
|
||||||
|
|
||||||
solve :: ([ℜ] -> [ℜ]) -> [ℜ] -> Int
|
solve :: ([ℜ] -> [ℜ]) -> [ℜ] -> Int
|
||||||
solve f s = let fixed = (\case
|
solve f s = let fixed = (\case
|
||||||
@ -54,6 +51,6 @@ solve f s = let fixed = (\case
|
|||||||
sum $ zipWith (*) fixed [0..]
|
sum $ zipWith (*) fixed [0..]
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = readFile "inputs/9.example" <&> parse >>= \i ->
|
main = readFile "inputs/9" <&> parse >>= \i ->
|
||||||
print (solve sort1 i) >>
|
print (solve sort1 i) >>
|
||||||
print (solve sort2 i)
|
print (solve sort2 i)
|
||||||
|
Loading…
Reference in New Issue
Block a user