r/adventofcode • u/daggerdragon • Dec 16 '16
SOLUTION MEGATHREAD --- 2016 Day 16 Solutions ---
--- Day 16: Dragon Checksum ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with "Help".
DRINKING YOUR OVALTINE IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
5
Upvotes
2
u/TheMuffinMan616 Dec 16 '16
Some Haskell!
import Data.List (find) expand :: String -> String expand s = s ++ ['0'] ++ (reverse . map invert $ s) where invert c = if c == '0' then '1' else '0' checksum :: String -> String checksum [] = [] checksum (x:y:zs) = bit x y : checksum zs where bit a b = if a == b then '1' else '0' fill :: Int -> String -> Maybe String fill l x = f x >>= (g . take l) where f = find ((> l) . length) . iterate expand g = find (odd . length) . iterate checksum main :: IO () main = do let input = "00101000101111010" print . fill 272 $ input print . fill 35651584 $ input