diff options
-rw-r--r-- | 11-1.hs | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -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 |