11 part 2 done
This commit is contained in:
parent
a20210b96f
commit
ad760a6c83
@ -122,6 +122,7 @@ executable 11
|
||||
-- other-extensions:
|
||||
build-depends: base ^>=4.18.2.1
|
||||
, split
|
||||
, containers
|
||||
hs-source-dirs: app
|
||||
default-language: GHC2021
|
||||
executable 12
|
||||
|
34
app/11.hs
34
app/11.hs
@ -1,37 +1,37 @@
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
module Main where
|
||||
|
||||
import Data.Functor
|
||||
import Data.List.Split
|
||||
import Data.IntMap (IntMap)
|
||||
import qualified Data.IntMap as IM
|
||||
|
||||
parse :: String -> [Int]
|
||||
parse = (read <$>) . words
|
||||
parse :: String -> IntMap Int
|
||||
parse = foldr (flip (IM.insertWith (+)) 1 . read) mempty . words
|
||||
|
||||
rules :: [(Int -> Bool, Int -> [Int])]
|
||||
rules :: [(Int -> Bool, Int -> IntMap Int)]
|
||||
rules =
|
||||
[
|
||||
((==0), const [1])
|
||||
,(even . length . show, \a -> let b = show a; s = length b in chunksOf (s `div` 2) b <&> read)
|
||||
,(const otherwise, pure . (*2024))
|
||||
((==0), const (IM.singleton 1 1))
|
||||
,(even . length . show,
|
||||
\a -> let b = show a; s = length b in
|
||||
foldr (flip (IM.insertWith (+)) 1 . read) mempty (chunksOf (s `div` 2) b))
|
||||
,(const otherwise, (`IM.singleton` 1) . (*2024))
|
||||
]
|
||||
|
||||
applyRule :: Int -> [Int]
|
||||
applyRule :: Int -> IntMap Int
|
||||
applyRule i = head . filter (/= mempty) $ rules >>= \(f,g) -> if f i then pure $ g i else pure mempty
|
||||
|
||||
step :: [Int] -> [Int]
|
||||
step = (applyRule =<<)
|
||||
step :: IntMap Int -> IntMap Int
|
||||
step = IM.foldrWithKey (\s c n -> IM.unionWith (+) n ((c*) <$> applyRule s)) mempty
|
||||
|
||||
applyAmount :: Int -> a -> (a -> a) -> a
|
||||
applyAmount 0 a _ = a
|
||||
applyAmount n a f = f $ applyAmount (n-1) a f
|
||||
|
||||
solve1 :: [Int] -> Int
|
||||
solve1 = length . flip (applyAmount 25) step
|
||||
|
||||
solve2 :: [Int] -> Int
|
||||
solve2 = undefined
|
||||
solve :: Int -> IntMap Int -> Int
|
||||
solve i = sum . IM.elems . flip (applyAmount i) step
|
||||
|
||||
main :: IO ()
|
||||
main = readFile "inputs/11" <&> parse >>= \i ->
|
||||
print (solve1 i) >>
|
||||
print (solve2 i)
|
||||
print (solve 25 i) >>
|
||||
print (solve 75 i)
|
||||
|
Loading…
Reference in New Issue
Block a user