58 lines
1014 B
Ruby
Executable File
58 lines
1014 B
Ruby
Executable File
#!/usr/bin/env ruby
|
|
|
|
INPUT = __dir__ + '/input'
|
|
OUTPUT = 19690720
|
|
|
|
def part1
|
|
File.readlines(INPUT).map { |line| execute(correct(to_opcode(line))).join(',') }
|
|
end
|
|
|
|
def part2
|
|
File.readlines(INPUT).map do |line|
|
|
opcodes = to_opcode(line)
|
|
(0..99).each do |i|
|
|
(0..99).each do |j|
|
|
opcodes[1] = i
|
|
opcodes[2] = j
|
|
result = execute(opcodes.dup)
|
|
return 100 * i + j if result[0] == OUTPUT
|
|
end
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
def to_opcode(line)
|
|
line.chomp.split(',').map(&:to_i)
|
|
end
|
|
|
|
def correct(opcodes)
|
|
opcodes[1] = 12
|
|
opcodes[2] = 2
|
|
opcodes
|
|
end
|
|
|
|
def execute(opcodes)
|
|
i = 0
|
|
while i < opcodes.size
|
|
opcode = opcodes[i]
|
|
|
|
if opcode == 1
|
|
opcodes[opcodes[i + 3]] = opcodes[opcodes[i + 1]] + opcodes[opcodes[i + 2]]
|
|
elsif opcode == 2
|
|
opcodes[opcodes[i + 3]] = opcodes[opcodes[i + 1]] * opcodes[opcodes[i + 2]]
|
|
else
|
|
break
|
|
end
|
|
|
|
i += 4
|
|
end
|
|
|
|
opcodes
|
|
end
|
|
|
|
puts "=== Part 1 ==="
|
|
puts part1
|
|
puts "=== Part 2 ==="
|
|
puts part2
|