Day 11: Plutonian Pebbles

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • LeixB@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    14 days ago

    Haskell

    import Data.Monoid
    import Control.Arrow
    
    data Tree v = Tree (Tree v) v (Tree v)
    
    -- https://stackoverflow.com/questions/3208258
    memo1 f = index nats
      where
        nats = go 0 1
        go i s = Tree (go (i + s) s') (f i) (go (i + s') s')
          where
            s' = 2 * s
        index (Tree l v r) i
            | i < 0 = f i
            | i == 0 = v
            | otherwise = case (i - 1) `divMod` 2 of
                (i', 0) -> index l i'
                (i', 1) -> index r i'
    
    memo2 f = memo1 (memo1 . f)
    
    blink = memo2 blink'
      where
        blink' c n
            | c == 0 = 1
            | n == 0 = blink c' 1
            | even digits = blink c' l <> blink c' r
            | otherwise = blink c' $ n * 2024
          where
            digits = succ . floor . logBase 10 . fromIntegral $ n
            (l, r) = n `divMod` (10 ^ (digits `div` 2))
            c' = pred c
    
    doBlinks n = getSum . mconcat . fmap (blink n)
    part1 = doBlinks 25
    part2 = doBlinks 75
    
    main = getContents >>= print . (part1 &&& part2) . fmap read . words