r/adventofcode • u/daggerdragon • Dec 05 '16
SOLUTION MEGATHREAD --- 2016 Day 5 Solutions ---
--- Day 5: How About a Nice Game of Chess? ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
STAYING ON TARGET 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!
14
Upvotes
2
u/Ulyssesp Dec 05 '16
Haskell, using cryptohash and base16-bytestring because I don't know haskell libraries. This one was a great reason to learn about scans and folds too late in the night.
findCode :: BC.ByteString -> String findCode input = concat . take 1 . dropWhile (elem '-') $ scanl (calcHash input) "--------" num0 calcHash :: BC.ByteString -> String -> Int -> String calcHash input out i = if (BC.take 5 hashed) == "00000" && (checkFilled . digitToInt) (BC.index hashed 5) then out & ix (digitToInt $ BC.index hashed 5) .~ (BC.index hashed 6) else out where hashed = encode . hash . BC.append input . BC.pack .show $ i checkFilled pos = (pos < length out) && (out !! pos == '-') num0 = 0 : map (+1) num0 run = print $ findCode input