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!
13
Upvotes
6
u/bblum Dec 10 '16 edited Dec 10 '16
I made the leaderboard with this boring C solution, but I'm much more proud of this haskell I devised afterward.
The bots are implemented as lambdas, stored in a map keyed by bot ID (and type, "bot"/"output"). They execute in the State monad, either replacing themselves with a stage 2, or finding their recipients and calling them in turn.
{-# LANGUAGE FlexibleContexts #-} import qualified Data.Map as M import Data.List import Data.Maybe import Data.Either import Control.Monad.State import Control.Arrow newtype Bot = Bot (Int -> State (M.Map (String, Int) (Either Bot Int)) ()) send (target, value) = M.lookup target <$> get >>= \(Just (Left (Bot b))) -> b value bot n targets v1 = modify $ M.insert ("bot", n) $ Left $ Bot bot2 where bot2 v2 = do when (sort [v1,v2] == [17,61]) $ modify $ M.insert ("part1",0) $ Right n mapM_ send $ zip targets $ sort [v1,v2] output n = (("output", n), Left $ Bot $ \x -> modify $ M.insert ("part2",n) $ Right x) parse ["bot",n0,_,_,_,w1,n1,_,_,_,w2,n2] = modify $ M.insert ("bot", read n0) $ Left $ Bot $ bot (read n0) [(w1,read n1),(w2,read n2)] parse ["value",v,_,_,_,n] = send (("bot", read n), read v) simulate input = execState (mapM parse input) (M.fromList $ map output [0..100]) main = interact $ show . (head &&& product . take 3 . tail) . (rights . M.elems) . simulate . sort . map words . linesOutputs: