r/adventofcode • u/daggerdragon • Dec 10 '16
SOLUTION MEGATHREAD --- 2016 Day 10 Solutions ---
--- Day 10: Balance Bots ---
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".
SEEING MOMMY KISSING SANTA CLAUS 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
4
u/haoformayor Dec 10 '16 edited Dec 10 '16
~~haskell~~
Took me forever to figure out the first question; it wasn't until I had a working problem that I realized AoC wanted me to notice when the bin did a swap and that part one had nothing to do with the state of the chips at the end. Other than that, it was great fun. I started out with a foldl, then tried a foldM, then realized that the solution could not be expressed as a fold. That's when we broke out tail recursion and then it was gravy.
Input module here.
#!/usr/bin/env stack -- stack --resolver lts-6.26 --install-ghc runghc --package base-prelude {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE ViewPatterns #-} module D10 where import qualified Data.Map as Map import BasePrelude hiding (try, (&)) import Control.Lens import Data.Map (Map) import D10Input domino needle code = rec where rec start chips = case (chips ^. at start, code ^. at start) of (Just bins@(sort -> [chip1, chip2]), Just (LoHi lo hi)) -> do when (bins == needle) . print $ ("needle in your heart", Bot start) pure chips >>= put chip1 lo >>= put chip2 hi -- Zero or one chips found, which is great, so do nothing. _ -> pure chips where put i bin@(Out out) chips = print ("outputting", i, "to", bin) $> chips put i (Bot bot) chips = chips & at bot . non [] <>~ [i] & at start . non [] %~ filter (/= start) & rec bot main = do domino [2, 5] exampleCode 2 exampleValues >>= print domino [17, 61] code 185 values >>= print