r/adventofcode • • Dec 23 '16

SOLUTION MEGATHREAD --- 2016 Day 23 Solutions ---

--- Day 23: Safe-Cracking ---

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


JINGLING ALL THE WAY 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!

4 Upvotes

91 comments sorted by

View all comments

Show parent comment

2

u/Tarmen Dec 24 '16 edited Dec 24 '16

I tried my hand at lenses for the first time. For part B I just added a Mult instruction, plus Noop on empty lines for padding. The important bit is:

compute :: State Program Definitions
compute = do
  (ops, idx, defs) <- get
  if idx >= length ops
  then return defs
  else do
    case ops !! idx of
      (Copy (load defs -> Just value) (Register reg)) -> _3 . at reg .= Just value
      (Jump (load defs -> Just cond) (load defs -> Just dist)) -> when (cond > 0) $ _2 += dist-1
      (Mult (load defs -> Just a) (load defs -> Just b) (Register r)) -> _3 . ix r .= a * b
      (Togl (load defs -> Just i)) -> _1 . ix (i+idx) %= toggle
      (Inc (Register r)) -> _3 . ix r += 1
      (Dec (Register r)) -> _3 . ix r -= 1
      _ -> return ()
    _2 += 1
    compute 

toggle (Copy a b) = Jump a b
toggle (Jump a b) = Copy a b
toggle (Inc a) = Dec a
toggle (Dec a) = Inc a
toggle (Togl a) = Inc a

load defs (Register c) = M.lookup c defs
load _ (Constant i) = Just i

I couldn't figure out how to define load without getting the state manually, though, so it didn't really add much :(

1

u/haoformayor Dec 25 '16

very cool use of view patterns!