summaryrefslogtreecommitdiff
path: root/11-1.hs
blob: de60536f2861491eb239c9699f9cb563dd9e6c3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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