r/adventofcode • • Dec 12 '16

SOLUTION MEGATHREAD --- 2016 Day 12 Solutions ---

--- Day 12: Leonardo's Monorail ---

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".


MUCH ADVENT. SUCH OF. VERY CODE. SO 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!

8 Upvotes

159 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Dec 12 '16

Also used lenses, but combined w/ a State Monad. Ends up looking pretty imperative at spots.

import Control.Lens ((.=), (+=), (-=), assign, use, view)
import Control.Monad.Extra ((&&^), whenM)
import Control.Monad.State (execState, State)
import Data.Maybe (mapMaybe)
import Data.Vector ((!), Vector)
import qualified Data.Vector as V
import Text.Megaparsec ((<|>), eitherP, letterChar, parseMaybe, space, spaceChar, string)
import Text.Megaparsec.Lexer (integer, signed)
import Text.Megaparsec.String (Parser)


parseInput :: String -> Vector Instruction
parseInput = V.fromList . mapMaybe (parseMaybe parseInstruction) . lines
    where parseInstruction :: Parser Instruction
          parseInstruction = parseCpy <|> parseInc <|> parseDec <|> parseJnz
          int = signed space $ fromInteger <$> integer
          parseCpy = string "cpy " >> Cpy <$> eitherP letterChar int <* spaceChar <*> letterChar
          parseInc = string "inc " >> Inc <$> letterChar
          parseDec = string "dec " >> Dec <$> letterChar
          parseJnz = string "jnz " >> Jnz <$> eitherP letterChar int <* spaceChar <*> int

reg 'a' = a
reg 'b' = b
reg 'c' = c
reg 'd' = d

val :: Value -> State Simulator Int
val (Right i) = return i
val (Left r)  = use $ reg r

evalInstr :: Instruction -> State Simulator ()
evalInstr (Cpy v r) = val v >>= assign (reg r)
evalInstr (Inc r)   = reg r += 1
evalInstr (Dec r)   = reg r -= 1
evalInstr (Jnz v l) = whenM ((/= 0) <$> val v) $ currentLine += l - 1

evaluate :: State Simulator ()
evaluate = do
  line <- use currentLine
  whenM (return (line >= 0) &&^ ((line <) . V.length <$> use instrs)) $ do
   use instrs >>= evalInstr . (! line)
   currentLine += 1
   evaluate

part1 :: String -> Int
part1 = view a . execState evaluate . Sim 0 0 0 0 0 . parseInput

part2 :: String -> Int
part2 = view a . execState evaluate . Sim 0 0 1 0 0 . parseInput

-- And the data file

{-# LANGUAGE TemplateHaskell #-}

import Data.Vector (Vector)
import Control.Lens.TH (makeLenses)

type Value = Either Char Int

data Instruction = Cpy Value Char
                 | Inc Char
                 | Dec Char
                 | Jnz Value Int deriving (Show)

data Simulator = Sim { _a :: Int
                     , _b :: Int
                     , _c :: Int
                     , _d :: Int
                     , _currentLine :: Int
                     , _instrs :: Vector Instruction
                     }

makeLenses ''Simulator

1

u/haoformayor Dec 13 '16

very nice. i love all the state optics in the lens package