Following j input causes limit error when 'noun verb'=. 0 17 , leaving the instruction pointer unchanged (0).  Uncommenting the line

   NB. if.nv-:0 17 do. continue. end.

advances the limit error to nv matches 0 35

However the input 0 17 produces the result I expect:

   decode^:_] 0 [MEMORY=: 1 2 (((nv=:'noun verb'=:0 17)"_))`[`]}PROGRAM
|uncaught throw.: halt
|       verb`:6 arguments

Here's my intcode computer:


NB. advent of code 2019 DEC 02 part 2.

PROGRAM=: _ ". LF -.~ (0 :0)
  1   0   0   3 1   1  2   3 1   3   4   3 1   5   0   3
  2   1   6  19 2  19  6  23 1  23   5  27 1   9  27  31
  1  31  10  35 2  35  9  39 1   5  39  43 2  43   9  47
  1   5  47  51 2  51 13  55 1  55  10  59 1  59  10  63
  2   9  63  67 1  67  5  71 2  13  71  75 1  75  10  79
  1  79   6  83 2  13 83  87 1  87   6  91 1   6  91  95
  1  10  95  99 2  99  6 103 1 103   5 107 2   6 107 111
  1  10 111 115 1 115  5 119 2   6 119 123 1 123   5 127
  2 127   6 131 1 131  5 135 1   2 135 139 1 139  13   0
 99   2   0  14 0
)

INSTRUCTION_TABLE=: ".;._2]0 :0
 (99"_)`halt`0:`('halt'"_)
 1:`add`3:`('add values in positions 1 2 store in 3'"_)
 2:`mul`3:`('multiply values in positions 1 2 store in 3'"_)
)
INSTRUCTION_INDEXES=: ({."1 INSTRUCTION_TABLE)`:0''
T=: /: INSTRUCTION_INDEXES
INSTRUCTION_TABLE=: T { INSTRUCTION_TABLE
INSTRUCTION_INDEXES=: T { INSTRUCTION_INDEXES

assert INSTRUCTION_INDEXES -: 1 2 99

NB. y is the instruction argument list
halt=: 3 :'throw.'
add=: 3 :'MEMORY=: y ([: +/ ({~ 2&{.)~)`({:@:[)`]} MEMORY'
mul=: 3 :'MEMORY=: y ([: */ ({~ 2&{.)~)`({:@:[)`]} MEMORY'

decode=: 3 :0 NB. evaluate the instruction at instruction pointer y, return the updated IP.
 operation=: INSTRUCTION_TABLE {~ INSTRUCTION_INDEXES I. y { MEMORY
 verb=: 1 { operation
 length=: (2 { operation)`:6''
 arguments=: MEMORY {~ (>: y) + i. length
 verb`:6 arguments
 NB. return the new instruction pointer
 >: y + length
)


3 :0''
 nv=: 0
 for_noun.i.100 do.
  for_verb.i.100 do.
   MEMORY=: 1 2 (((nv=:noun,verb)"_))`[`]}PROGRAM
   INS=: 0
   NB. if.nv-:0 17 do. continue. end.
   try.
    while.1 do.
     INS=: decode INS
    end.
   catcht.
    if.19690720=0{MEMORY do.
     noun*100 + verb
     return.
    end.
   end.
  end.
 end.
)

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to