From 310a752668c80f13803adff665090a868f54b1ab Mon Sep 17 00:00:00 2001 From: Laura Orvokki Kursula Date: Wed, 11 Dec 2024 16:24:27 +0100 Subject: 11-2 Sadly did not manage this one without help. Had to look at Reddit to figure out the trick with the hashmap... --- 11-2.hs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 11-2.hs diff --git a/11-2.hs b/11-2.hs new file mode 100644 index 0000000..d6c61aa --- /dev/null +++ b/11-2.hs @@ -0,0 +1,53 @@ +import Data.Array +import Data.HashMap.Strict (HashMap, fromListWith, toList) + +transform :: Int -> Either Int (Int,Int) +transform n + | even (digits n) = Right $ halves n + | otherwise = Left $ n * 2024 + +digits :: Int -> Int +digits n = floor $ logBase 10 (fromIntegral n) + 1 + +halves :: Int -> (Int,Int) +halves n = ( floor $ fromIntegral n / 10^x + , n `mod` 10^x + ) + where + x = digits n `div` 2 + +next :: Array Int (Either Int (Int,Int)) +next = array (0, maxIdx) + $ (0, Left 1) : [ (n, transform n) | n <- [1..maxIdx] ] + +maxIdx :: Int +maxIdx = 10^4 + +getNext :: Int -> Either Int (Int,Int) +getNext x | x <= maxIdx = next ! x + | otherwise = transform x + +depth :: Int +depth = 75 + +evalList :: [(Int,Int)] -> [(Int,Int)] +evalList = go [] + where + go :: [(Int,Int)] -> [(Int,Int)] -> [(Int,Int)] + go res [] = res + go res ((x,c):xs) = case getNext x of + Left y -> go ((y,c):res) xs + Right (y,z) -> go ((y,c):(z,c):res) xs + +evalHM :: HashMap Int Int -> HashMap Int Int +evalHM = fromListWith (+) . evalList . toList + +main :: IO () +main = getLine + >>= print + . sum + . (!! depth) + . iterate evalHM + . fromListWith (+) + . map ((,1) . read) + . words -- cgit v1.2.3