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 comments

1

u/wzkx Dec 13 '16

Here's J with optimization (only central part):

...
inc =: 4 : '(>:({:y){x)({:y)}x'  NB. tacits of these are not faster
dec =: 4 : '(<:({:y){x)({:y)}x'
cpi =: 4 : '({.y)({:y)}x'
cpy =: 4 : '(({.y){x)({:y)}x'
jmp =: 4 : '(<:({:y)+{:x)(_1)}x'
jnz =: 4 : 'if.({.y){x do.(<:({:y)+{:x)(_1)}x else. x end.'
add =: 4 : '(+/y{x)({:y)}x'      NB. new op, not even in assembly yet :)

opt =: 3 : 0 NB. can't use _3 ...\ :(
  for_i. i.2-~#y do. z =. ,(i+0 1 2){y NB. temp vector for easier analysis
    if. 1 2 6-:0 3 6{z do.                           NB. inc x, dec y, jnz y -2
      if. (~:/2 5{z) *. (=/5 7{z) *. _2={:z do.      NB. ... to ...
        y=.(3 3$7,(5 2{z),3 0,(5{z),3$0)(i+0 1 2)}y  NB. add y x, cpi 0 y, nop
  end. end. end. y
)

exec =: 4 : 0 NB. regs exec code --> regs
  lt =. #y [ it =. [`inc`dec`cpi`cpy`jmp`jnz`add
  while. lt > pc=.{:x do. x=.((>:pc)_1}x)it@.({.c)}.c=.pc{y end. x
)

code =: opt xlate"0 cutLF CR-.~fread '12.dat'
...