36 lines
722 B
Ruby
Executable File
36 lines
722 B
Ruby
Executable File
#!/usr/bin/env ruby
|
|
|
|
def process_opcodes(list)
|
|
index = 0
|
|
|
|
while index < (list.length - 1)
|
|
opcode = list[index]
|
|
case opcode
|
|
when 1
|
|
input1 = list[list[index+1]]
|
|
input2 = list[list[index+2]]
|
|
list[list[index+3]] = input1 + input2
|
|
when 2
|
|
input1 = list[list[index+1]]
|
|
input2 = list[list[index+2]]
|
|
list[list[index+3]] = input1 * input2
|
|
when 99
|
|
break
|
|
end
|
|
index += 4
|
|
end
|
|
return list
|
|
end
|
|
|
|
ARGF.each do |line|
|
|
# Eat the opcode list
|
|
list = line.split(",").map(&:to_i)
|
|
|
|
# Fix the program as per the exercise
|
|
list[1] = 12
|
|
list[2] = 2
|
|
|
|
# Print the post processed list for inspection to get the 0th element
|
|
puts process_opcodes(list).inspect
|
|
end
|