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
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
You are viewing a single thread.
View all comments 3 points
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