52 lines
1.0 KiB
Ruby
Executable File
52 lines
1.0 KiB
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)
|
|
|
|
noun = 0
|
|
verb = 0
|
|
target_number = 19690720
|
|
program_output = [0]
|
|
|
|
while program_output[0] != target_number
|
|
temp_list = list.dup
|
|
temp_list[1] = noun
|
|
temp_list[2] = verb
|
|
|
|
# Enumarate through all possible nouns and verbs O(n^2)
|
|
if noun < 99
|
|
noun += 1
|
|
else
|
|
noun = 0
|
|
verb += 1
|
|
end
|
|
|
|
program_output = process_opcodes(temp_list)
|
|
end
|
|
# Print the post processed list for inspection to get the 0th element
|
|
puts program_output.inspect
|
|
end
|