summaryrefslogtreecommitdiff
path: root/2024/11.hs
blob: 1191d1734acddcbd0a33f725af213cfc4af5962b (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
import qualified Data.Map.Strict as Map

main :: IO ()
main = do
  s0 <- map (read @Int) . words <$> getContents
  let ndigs10 0 = 1
      ndigs10 n = pre (4::Int)
        where pre p | n >= 10 ^ p = pre (2*p)
                    | otherwise = bin 0 p
              bin lo hi | lo == hi - 1 = hi
                        | n >= 10 ^ mid = bin mid hi
                        | otherwise = bin lo mid
                where mid = (lo + hi) `quot` 2
  let splitHalf n =
        let nd = ndigs10 n
        in if even nd then Just $ n `quotRem` (10 ^ (ndigs10 n `quot` 2))
                      else Nothing
  let blink =
        Map.fromListWith (+)
        . concatMap (\(n, c) ->
            if n == 0 then [(1, c)]
            else case splitHalf n of
                   Just (n1, n2) -> [(n1, c), (n2, c)]
                   Nothing -> [(n * 2024, c)])
        . Map.assocs
  print $ sum . Map.elems $ iterate blink (Map.fromList (map (,1::Int) s0)) !! 25
  print $ sum . Map.elems $ iterate blink (Map.fromList (map (,1) s0)) !! 75