2019: Day 5 Part 2
This commit is contained in:
parent
608b4078c4
commit
1ae1c260f1
2 changed files with 79 additions and 16 deletions
|
|
@ -2,22 +2,18 @@
|
||||||
|
|
||||||
module Intcode
|
module Intcode
|
||||||
def self.read(filename)
|
def self.read(filename)
|
||||||
input = File.read(filename)
|
parse File.read(filename)
|
||||||
.gsub(/^#.*$/, '')
|
|
||||||
.gsub('\s', '')
|
|
||||||
Program.new(parse(input))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.parse(input)
|
def self.parse(input)
|
||||||
input.split(',').map(&:to_i)
|
cleaned = input
|
||||||
|
.gsub(/^#.*$/, '')
|
||||||
|
.gsub('\s', '')
|
||||||
|
Program.new cleaned.split(',').map(&:to_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
class Program
|
class Memory < Array
|
||||||
attr_accessor :mem
|
def to_s
|
||||||
def initialize(mem)
|
|
||||||
@mem = mem.clone
|
|
||||||
|
|
||||||
def @mem.to_s
|
|
||||||
batch_size = 10
|
batch_size = 10
|
||||||
output = String.new
|
output = String.new
|
||||||
each_slice(batch_size).each_with_index do |batch, idx|
|
each_slice(batch_size).each_with_index do |batch, idx|
|
||||||
|
|
@ -28,7 +24,19 @@ module Intcode
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Program
|
||||||
|
attr_accessor :mem
|
||||||
|
def initialize(mem)
|
||||||
|
@input = nil
|
||||||
|
@initial_state = mem
|
||||||
|
end
|
||||||
|
|
||||||
|
def on_input(&block)
|
||||||
|
@input = block
|
||||||
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
|
@mem = Memory.new(@initial_state.clone)
|
||||||
pc = 0
|
pc = 0
|
||||||
loop do
|
loop do
|
||||||
opcode = operation(mem[pc])
|
opcode = operation(mem[pc])
|
||||||
|
|
@ -66,6 +74,46 @@ module Intcode
|
||||||
puts "output: #{output}"
|
puts "output: #{output}"
|
||||||
|
|
||||||
pc += 2
|
pc += 2
|
||||||
|
when 5
|
||||||
|
r1 = mem[pc + 1]
|
||||||
|
r2 = mem[pc + 2]
|
||||||
|
a = param(r1, mode[0])
|
||||||
|
b = param(r2, mode[1])
|
||||||
|
if !a.zero?
|
||||||
|
pc = b
|
||||||
|
else
|
||||||
|
pc += 3
|
||||||
|
end
|
||||||
|
when 6
|
||||||
|
r1 = mem[pc + 1]
|
||||||
|
r2 = mem[pc + 2]
|
||||||
|
a = param(r1, mode[0])
|
||||||
|
b = param(r2, mode[1])
|
||||||
|
if a.zero?
|
||||||
|
pc = b
|
||||||
|
else
|
||||||
|
pc += 3
|
||||||
|
end
|
||||||
|
when 7
|
||||||
|
r1 = mem[pc + 1]
|
||||||
|
r2 = mem[pc + 2]
|
||||||
|
r3 = mem[pc + 3]
|
||||||
|
a = param(r1, mode[0])
|
||||||
|
b = param(r2, mode[1])
|
||||||
|
raise StandardError, "unexpected mode: #{mode[2]}" if mode[2] != 0
|
||||||
|
|
||||||
|
mem[r3] = a < b ? 1 : 0
|
||||||
|
pc += 4
|
||||||
|
when 8
|
||||||
|
r1 = mem[pc + 1]
|
||||||
|
r2 = mem[pc + 2]
|
||||||
|
r3 = mem[pc + 3]
|
||||||
|
a = param(r1, mode[0])
|
||||||
|
b = param(r2, mode[1])
|
||||||
|
raise StandardError, "unexpected mode: #{mode[2]}" if mode[2] != 0
|
||||||
|
|
||||||
|
mem[r3] = a == b ? 1 : 0
|
||||||
|
pc += 4
|
||||||
when 99
|
when 99
|
||||||
return mem[0]
|
return mem[0]
|
||||||
else
|
else
|
||||||
|
|
@ -78,6 +126,14 @@ module Intcode
|
||||||
|
|
||||||
def input
|
def input
|
||||||
print 'input> '
|
print 'input> '
|
||||||
|
return default_input if @input.nil?
|
||||||
|
|
||||||
|
inp = @input.call
|
||||||
|
puts inp
|
||||||
|
inp
|
||||||
|
end
|
||||||
|
|
||||||
|
def default_input
|
||||||
gets.to_i
|
gets.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
7
2019/day5/part2.rb
Normal file
7
2019/day5/part2.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative 'intcode'
|
||||||
|
|
||||||
|
input = File.join(__dir__, 'input')
|
||||||
|
program = Intcode.read(input)
|
||||||
|
program.run { 5 }
|
||||||
Loading…
Reference in a new issue