Say we have the following code:
def run(input)
start = 0
lap = 0
while true
opcode = input[start]
input1 = input[start + 1]
input2 = input[start + 2]
output = input[start + 3]
if opcode == 1
input[output] = input[input1] + input[input2]
elsif opcode == 2
input[output] = input[input1] * input[input2]
elsif opcode == 99
puts "breaking"
break
else
puts "CANT RECOGNIZE THAT OPCODE"
end
start += 4
lap += 1
end
puts input
puts "finished after #{lap} loops"
end
input = [1,9,10,3,2,3,11,0,99,30,40,50]
run(input)
I understand why printing input1
I get the number stored in that possition in the array (9), but I don't get why printing input[input1]
(input[input[start + 1]]
) accesses the value the number in that position (9) points to (30).
The code works for the exercise but I don't understand why.