diff options
author | Laura Orvokki Kursula <lav@vampires.gay> | 2024-12-11 10:22:10 +0100 |
---|---|---|
committer | Laura Orvokki Kursula <lav@vampires.gay> | 2024-12-11 10:22:10 +0100 |
commit | 109578eaa586a23830c0ae43a9f1418b77a27be1 (patch) | |
tree | 016018f4331d936be4fd616a2e6042e36c8b78c8 | |
parent | cff3c9c15bce92c6697ef13be2485d6bcf7291ed (diff) | |
download | aoc2024-109578eaa586a23830c0ae43a9f1418b77a27be1.tar.gz aoc2024-109578eaa586a23830c0ae43a9f1418b77a27be1.zip |
11-1
-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 |