This commit is contained in:
toastyandwarm 2024-12-12 15:29:13 +00:00
parent befef25ecc
commit 3d65246b2d
6 changed files with 135 additions and 7 deletions

BIN
11/solution Executable file

Binary file not shown.

BIN
11/solution.hi Normal file

Binary file not shown.

View file

@ -22,8 +22,8 @@ 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)
solve2 :: [Int] -> Integer
solve2 xs = sum $ map (memocount 40000) xs -- ((map ((memoize2 stoneCount) 40000) xs) `using` parList rseq)
stoneStep :: Int -> [Int]
stoneStep 0 = [1]
@ -35,14 +35,21 @@ 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
stoneCount :: Int -> Int -> Integer
stoneCount 0 _ = 1
stoneCount n x
| x == 0 = memocount (n-1) 1
| (floor $ logBase 10 $ fromIntegral x) `rem` 2 == 1 = let d = ((\x -> x `div` 2) $ (+1) $ floor $ logBase 10 $ fromIntegral x) in (memocount (n-1) (x `rem` 10^d)) + (memocount (n-1) (x `div` 10^d))
| otherwise = memocount (n-1) (x*2024)
-- sum . map (((memoize2 stoneCount)) (n-1)) . stoneStep
memocount = memoize2 stoneCount
main :: IO()
main = do
fileinp <- readFile "input.txt"
let parsed = splitInput fileinp
--fileinp <- readFile "input.txt"
--let parsed = splitInput fileinp
let parsed = [1]
let solved1 = solve1 parsed
let solved2 = solve2 parsed
print solved1

BIN
11/solution.o Normal file

Binary file not shown.