From befef25ecc75bc2fa050848ec6e08068fb87f8a9 Mon Sep 17 00:00:00 2001 From: toastyandwarm Date: Wed, 11 Dec 2024 13:13:19 +0000 Subject: [PATCH] day 11 --- 11/solution.hs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 11/solution.hs diff --git a/11/solution.hs b/11/solution.hs new file mode 100644 index 0000000..edde06f --- /dev/null +++ b/11/solution.hs @@ -0,0 +1,49 @@ +import Data.List +import Data.Function.Memoize +import Debug.Trace +import Control.Parallel.Strategies + +traceOut :: Show a => a -> a +traceOut x = (traceShow x x) + +addFirst :: [[a]] -> a -> [[a]] +addFirst [] x = [[x]] +addFirst (y:ys) x = ([x] ++ y) : ys + +split :: [Char] -> String -> [String] +split _ [] = [] +split delims (x:xs) + | elem x delims = [[]] ++ split delims xs + | otherwise = addFirst (split delims xs) x + +splitInput :: String -> [Int] +splitInput = map read . split [' '] . head . split ['\n'] + +solve1 :: [Int] -> Int +solve1 = length . foldl (.) id (take 25 $ repeat (concat . map stoneStep)) + +solve2 :: [Int] -> Int +solve2 xs = sum $ ((map ((memoize2 stoneCount) 75) xs) `using` parList rseq) + +stoneStep :: Int -> [Int] +stoneStep 0 = [1] +stoneStep x + | (floor $ logBase 10 $ fromIntegral x) `rem` 2 == 1 = let d = ((\x -> x `div` 2) $ (+1) $ floor $ logBase 10 $ fromIntegral x) in [x `div` 10^d, x `rem` 10^d] + | otherwise = [x*2024] + +splitAtIndex :: Int -> [a] -> [[a]] +splitAtIndex 0 xs = [[], xs] +splitAtIndex a (x:xs) = addFirst (splitAtIndex (a-1) xs) x + +stoneCount :: Int -> Int -> Int +stoneCount 0 = (\x -> 1) +stoneCount n = sum . map (((memoize2 stoneCount)) (n-1)) . stoneStep + +main :: IO() +main = do + fileinp <- readFile "input.txt" + let parsed = splitInput fileinp + let solved1 = solve1 parsed + let solved2 = solve2 parsed + print solved1 + print solved2