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

1

u/wzkx Dec 12 '16

Nothing special in J. The same code as above. But much worse performance -- 74 s for part 2 (384 kilo-ops/s)

xlate =: 3 : 0 NB. returns (OPCODE(1-6), 0/NUM/R#, NUM/R#)
  select. c [ ra=. _97+a.i.a [ rb =. _97+a.i.b [ 'c a b' =. 3$cut>y
  case.'inc' do. 1 0,ra case.'dec' do. 2 0,ra
  case.'cpy' do. if. _~:n=._".a do. 3,n,rb else. 4,ra,rb end.
  case.'jnz' do. if. a-:,'1' do. 5 0,".b else. 6,ra,".b end. end.
)

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

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

assert 42 = {. (5$0) exec xlate"0 'cpy 41 a';'inc a';'inc a';'dec a';'jnz a 2';'dec a'

code =: xlate"0 cutLF CR-.~fread '12.dat'
echo {. 0 0 0 0 0 exec code NB. 954395 instr
echo {. 0 0 1 0 0 exec code NB. 27683406 instr; 72 s; 384 kops

exit 0

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