day 11
This commit is contained in:
parent
ff15d9e75e
commit
befef25ecc
1 changed files with 49 additions and 0 deletions
49
11/solution.hs
Normal file
49
11/solution.hs
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue