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/oantolin Dec 16 '16 edited Dec 16 '16

In Common Lisp writing a compiler instead is super-easy:

(defun asm (inp)
  (flet ((lbl (n) (intern (format nil "lbl~a" n))))
    (let* ((prog (loop
                    for line = (read-line inp nil)
                    while line
                    collect (read-from-string (format nil "(~a)" line))))
           (targets (loop
                       for n from 1
                       for (op x y) in prog
                       when (eq op 'jnz)
                       collect (+ n y)))
           (code nil))
      (loop
         for n from 1
         for (op x y) in prog
         do (progn
              (when (member n targets)
                (push (lbl n) code))
              (push (case op
                      ((inc) `(incf ,x))
                      ((dec) `(decf ,x))
                      ((cpy) `(setf ,y ,x))
                      ((jnz) `(unless (zerop ,x)
                                (go ,(lbl (min (length prog) (+ n y)))))))
                    code)))
      (push (lbl (length prog)) code)
      `(lambda (c)
         (let ((a 0) (b 0) (d 0))
           (tagbody
              ,@(reverse code))
           a)))))

Testing:

CL-USER> (with-input-from-string (in "cpy 41 a
inc a
inc a
dec a
jnz a 2
dec a") (asm in))
(LAMBDA (C)
  (LET ((A 0) (B 0) (D 0))
    (TAGBODY
      (SETF A 41)
      (INCF A)
      (INCF A)
      (DECF A)
      (UNLESS (ZEROP A) (GO |lbl6|))
      (DECF A)
     |lbl6|)
    A))

The resulting function runs pretty fast, too:

CL-USER> (time (funcall (eval (with-open-file (in "day12.txt") (asm in))) 1))
Evaluation took:
  0.060 seconds of real time
  0.060982 seconds of total run time (0.060982 user, 0.000000 system)
  101.67% CPU
  1 form interpreted
  6 lambdas converted
  186,470,926 processor cycles
  490,816 bytes consed

9227663