Completed days 1 and 2
This commit is contained in:
1
02/input
Normal file
1
02/input
Normal file
@@ -0,0 +1 @@
|
||||
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,19,5,23,1,6,23,27,1,27,5,31,2,31,10,35,2,35,6,39,1,39,5,43,2,43,9,47,1,47,6,51,1,13,51,55,2,9,55,59,1,59,13,63,1,6,63,67,2,67,10,71,1,9,71,75,2,75,6,79,1,79,5,83,1,83,5,87,2,9,87,91,2,9,91,95,1,95,10,99,1,9,99,103,2,103,6,107,2,9,107,111,1,111,5,115,2,6,115,119,1,5,119,123,1,123,2,127,1,127,9,0,99,2,0,14,0
|
||||
35
02/solution1.rb
Executable file
35
02/solution1.rb
Executable file
@@ -0,0 +1,35 @@
|
||||
#!/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
|
||||
51
02/solution2.rb
Executable file
51
02/solution2.rb
Executable file
@@ -0,0 +1,51 @@
|
||||
#!/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
|
||||
Reference in New Issue
Block a user