2019: Day 7 Part 2
This commit is contained in:
parent
8c4cc61e3b
commit
402edd8b37
2 changed files with 77 additions and 1 deletions
|
|
@ -84,20 +84,27 @@ module Intcode
|
||||||
case operation(@mem[@ip])
|
case operation(@mem[@ip])
|
||||||
when 1
|
when 1
|
||||||
add!
|
add!
|
||||||
|
nil
|
||||||
when 2
|
when 2
|
||||||
mul!
|
mul!
|
||||||
|
nil
|
||||||
when 3
|
when 3
|
||||||
input!(&@input_block)
|
input!(&@input_block)
|
||||||
|
nil
|
||||||
when 4
|
when 4
|
||||||
output!(&@output_block)
|
output!(&@output_block)
|
||||||
when 5
|
when 5
|
||||||
branch_if_not_zero!
|
branch_if_not_zero!
|
||||||
|
nil
|
||||||
when 6
|
when 6
|
||||||
branch_if_zero!
|
branch_if_zero!
|
||||||
|
nil
|
||||||
when 7
|
when 7
|
||||||
less_than!
|
less_than!
|
||||||
|
nil
|
||||||
when 8
|
when 8
|
||||||
equal!
|
equal!
|
||||||
|
nil
|
||||||
when 99
|
when 99
|
||||||
:done_executing
|
:done_executing
|
||||||
else
|
else
|
||||||
|
|
@ -143,6 +150,7 @@ module Intcode
|
||||||
output = read(@ip + 1, mode[0])
|
output = read(@ip + 1, mode[0])
|
||||||
user_output.call output if block_given?
|
user_output.call output if block_given?
|
||||||
@ip += 2
|
@ip += 2
|
||||||
|
output
|
||||||
end
|
end
|
||||||
|
|
||||||
def branch_if_not_zero!
|
def branch_if_not_zero!
|
||||||
|
|
@ -184,7 +192,7 @@ module Intcode
|
||||||
def input
|
def input
|
||||||
if block_given?
|
if block_given?
|
||||||
inp = yield
|
inp = yield
|
||||||
raise "Invalid input: #{inp}" unless inp.is_a?(Numeric)
|
raise "Invalid input: #{inp.inspect}" unless inp.is_a?(Numeric)
|
||||||
|
|
||||||
return inp
|
return inp
|
||||||
end
|
end
|
||||||
|
|
|
||||||
68
2019/day7/part2.rb
Normal file
68
2019/day7/part2.rb
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative 'intcode'
|
||||||
|
|
||||||
|
def inputter(queue)
|
||||||
|
idx = 0
|
||||||
|
proc do
|
||||||
|
input = queue[idx]
|
||||||
|
idx += 1
|
||||||
|
input
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Amplifier
|
||||||
|
def initialize(memory, phase)
|
||||||
|
@input_queue = [phase]
|
||||||
|
@computer = Intcode::Computer.new(memory) { @input_queue.shift }
|
||||||
|
end
|
||||||
|
|
||||||
|
def amplify(input)
|
||||||
|
@input_queue << input
|
||||||
|
output = nil
|
||||||
|
output = @computer.tick until output
|
||||||
|
output
|
||||||
|
end
|
||||||
|
|
||||||
|
def input(val)
|
||||||
|
@input_queue << val
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def thrust(program, phases)
|
||||||
|
a = Amplifier.new(program.as_memory, phases[0])
|
||||||
|
b = Amplifier.new(program.as_memory, phases[1])
|
||||||
|
c = Amplifier.new(program.as_memory, phases[2])
|
||||||
|
d = Amplifier.new(program.as_memory, phases[3])
|
||||||
|
e = Amplifier.new(program.as_memory, phases[4])
|
||||||
|
|
||||||
|
cache = -Float::INFINITY
|
||||||
|
out1 = a.amplify(0)
|
||||||
|
loop do
|
||||||
|
out2 = b.amplify(out1)
|
||||||
|
out3 = c.amplify(out2)
|
||||||
|
out4 = d.amplify(out3)
|
||||||
|
out5 = e.amplify(out4)
|
||||||
|
return cache if out5 == :done_executing
|
||||||
|
|
||||||
|
cache = out5
|
||||||
|
out1 = a.amplify(out5)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
program = Intcode.read(File.join(__dir__, 'input'))
|
||||||
|
|
||||||
|
phase_space = [5, 6, 7, 8, 9].permutation(5)
|
||||||
|
|
||||||
|
max_thrust = -Float::INFINITY
|
||||||
|
max_phases = nil
|
||||||
|
phase_space.each do |phases|
|
||||||
|
t = thrust(program, phases)
|
||||||
|
next unless t > max_thrust
|
||||||
|
|
||||||
|
max_thrust = t
|
||||||
|
max_phases = phases
|
||||||
|
end
|
||||||
|
|
||||||
|
pp max_phases
|
||||||
|
puts max_thrust
|
||||||
Loading…
Reference in a new issue