From 109578eaa586a23830c0ae43a9f1418b77a27be1 Mon Sep 17 00:00:00 2001 From: Laura Orvokki Kursula Date: Wed, 11 Dec 2024 10:22:10 +0100 Subject: 11-1 --- 11-1.hs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 11-1.hs diff --git a/11-1.hs b/11-1.hs new file mode 100644 index 0000000..de60536 --- /dev/null +++ b/11-1.hs @@ -0,0 +1,37 @@ +data Tree = Node Int | Branch [Tree] + +toList :: Tree -> [Int] +toList (Node n) = [n] +toList (Branch ts) = concatMap toList ts + +digits :: Int -> Int +digits n = floor $ logBase 10 (fromIntegral n) + 1 + +transform :: Tree -> Tree +transform (Node 0) = Node 1 +transform (Node n) + | even (digits n) = Branch . map Node $ halves n + | otherwise = Node $ n * 2024 +transform (Branch ts) = Branch $ fmap transform ts + +halves :: Int -> [Int] +halves n = [ floor $ fromIntegral n / 10^x + , n `mod` 10^x + ] + where + x = digits n `div` 2 + +parse :: String -> Tree +parse = Branch . map (Node . read) . words + +flatten :: Tree -> Tree +flatten = Branch . map Node . toList + +main :: IO () +main = getLine + >>= print + . length + . toList + . (!!25) + . iterate (flatten . transform) + . parse -- cgit v1.2.3