2019: Day 11 Part 1
This commit is contained in:
parent
22719458d7
commit
a2208a8f5f
2 changed files with 370 additions and 0 deletions
243
2019/day11/intcode.rb
Normal file
243
2019/day11/intcode.rb
Normal file
|
|
@ -0,0 +1,243 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'forwardable'
|
||||||
|
|
||||||
|
module Intcode
|
||||||
|
def self.read(filename)
|
||||||
|
parse File.read(filename)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.parse(input)
|
||||||
|
parsed = input
|
||||||
|
.gsub(/^#.*$/, '')
|
||||||
|
.gsub('\s', '')
|
||||||
|
.split(',')
|
||||||
|
.map(&:to_i)
|
||||||
|
Program.new parsed
|
||||||
|
end
|
||||||
|
|
||||||
|
class Program
|
||||||
|
def initialize(mem)
|
||||||
|
@initial_state = mem
|
||||||
|
end
|
||||||
|
|
||||||
|
def as_memory
|
||||||
|
Memory.new(@initial_state.clone)
|
||||||
|
end
|
||||||
|
|
||||||
|
def run(&user_input)
|
||||||
|
Computer.new(as_memory).run(&user_input)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Memory
|
||||||
|
extend Forwardable
|
||||||
|
|
||||||
|
def_delegator :@cells, :[]
|
||||||
|
def_delegator :@cells, :[]=
|
||||||
|
|
||||||
|
attr_accessor :relative_base
|
||||||
|
|
||||||
|
def initialize(cells)
|
||||||
|
@cells = cells
|
||||||
|
@relative_base = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
batch_size = 10
|
||||||
|
s = String.new
|
||||||
|
@cells.each_slice(batch_size).each_with_index do |batch, idx|
|
||||||
|
prefix = idx * batch_size
|
||||||
|
s << "#{prefix}: #{batch.join(' ')}\n"
|
||||||
|
end
|
||||||
|
s
|
||||||
|
end
|
||||||
|
|
||||||
|
def read(addr, mode)
|
||||||
|
# raise ArgumentError, "address out of range: #{addr}" if addr.negative? || addr >= @cells.length
|
||||||
|
return 0 if addr.negative? || addr >= @cells.length
|
||||||
|
|
||||||
|
case mode
|
||||||
|
when INDIRECT_MODE
|
||||||
|
read(@cells[addr], IMMEDIATE_MODE)
|
||||||
|
when IMMEDIATE_MODE
|
||||||
|
@cells[addr]
|
||||||
|
when RELATIVE_MODE
|
||||||
|
read(@cells[addr] + relative_base, IMMEDIATE_MODE)
|
||||||
|
else
|
||||||
|
raise ArgumentError, "unexpected mode: #{mode}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def write(addr, mode, val)
|
||||||
|
raise ArgumentError, "address out of range: #{addr}" if addr.negative? || addr >= @cells.length
|
||||||
|
|
||||||
|
case mode
|
||||||
|
when INDIRECT_MODE
|
||||||
|
r = @cells[addr]
|
||||||
|
@cells[r] = val
|
||||||
|
when IMMEDIATE_MODE
|
||||||
|
raise ArgumentError, 'writes are not allowed in immediate mode'
|
||||||
|
@cells[addr]
|
||||||
|
when RELATIVE_MODE
|
||||||
|
r = @cells[addr] + relative_base
|
||||||
|
@cells[r] = val
|
||||||
|
else
|
||||||
|
raise ArgumentError, "unexpected mode: #{mode}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
INDIRECT_MODE = 0
|
||||||
|
IMMEDIATE_MODE = 1
|
||||||
|
RELATIVE_MODE = 2
|
||||||
|
|
||||||
|
class Computer
|
||||||
|
def initialize(mem, &block)
|
||||||
|
@mem = mem
|
||||||
|
@ip = 0
|
||||||
|
@input_block = block
|
||||||
|
end
|
||||||
|
|
||||||
|
def on_output(&block)
|
||||||
|
@output_block = block
|
||||||
|
end
|
||||||
|
|
||||||
|
def run
|
||||||
|
loop do
|
||||||
|
return if tick == :done_executing
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def tick
|
||||||
|
opcode = operation(@mem[@ip])
|
||||||
|
case opcode
|
||||||
|
when 1
|
||||||
|
add!
|
||||||
|
nil
|
||||||
|
when 2
|
||||||
|
mul!
|
||||||
|
nil
|
||||||
|
when 3
|
||||||
|
input!(&@input_block)
|
||||||
|
nil
|
||||||
|
when 4
|
||||||
|
output!(&@output_block)
|
||||||
|
when 5
|
||||||
|
branch_if_not_zero!
|
||||||
|
nil
|
||||||
|
when 6
|
||||||
|
branch_if_zero!
|
||||||
|
nil
|
||||||
|
when 7
|
||||||
|
less_than!
|
||||||
|
nil
|
||||||
|
when 8
|
||||||
|
equal!
|
||||||
|
nil
|
||||||
|
when 9
|
||||||
|
move_relative_base!
|
||||||
|
when 99
|
||||||
|
:done_executing
|
||||||
|
else
|
||||||
|
raise StandardError, "unexpected opcode: #{opcode}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def add!
|
||||||
|
mode = modes(@mem[@ip])
|
||||||
|
a = @mem.read(@ip + 1, mode[0])
|
||||||
|
b = @mem.read(@ip + 2, mode[1])
|
||||||
|
@mem.write(@ip + 3, mode[2], a + b)
|
||||||
|
@ip += 4
|
||||||
|
end
|
||||||
|
|
||||||
|
def mul!
|
||||||
|
mode = modes(@mem[@ip])
|
||||||
|
a = @mem.read(@ip + 1, mode[0])
|
||||||
|
b = @mem.read(@ip + 2, mode[1])
|
||||||
|
@mem.write(@ip + 3, mode[2], a * b)
|
||||||
|
@ip += 4
|
||||||
|
end
|
||||||
|
|
||||||
|
def input!(&user_input)
|
||||||
|
mode = modes(@mem[@ip])
|
||||||
|
inp = input(&user_input)
|
||||||
|
@mem.write(@ip + 1, mode[0], inp)
|
||||||
|
@ip += 2
|
||||||
|
end
|
||||||
|
|
||||||
|
def output!(&user_output)
|
||||||
|
mode = modes(@mem[@ip])
|
||||||
|
output = @mem.read(@ip + 1, mode[0])
|
||||||
|
user_output.call output if block_given?
|
||||||
|
@ip += 2
|
||||||
|
output
|
||||||
|
end
|
||||||
|
|
||||||
|
def branch_if_not_zero!
|
||||||
|
mode = modes(@mem[@ip])
|
||||||
|
a = @mem.read(@ip + 1, mode[0])
|
||||||
|
b = @mem.read(@ip + 2, mode[1])
|
||||||
|
@ip = a.zero? ? @ip + 3 : b
|
||||||
|
end
|
||||||
|
|
||||||
|
def branch_if_zero!
|
||||||
|
mode = modes(@mem[@ip])
|
||||||
|
a = @mem.read(@ip + 1, mode[0])
|
||||||
|
b = @mem.read(@ip + 2, mode[1])
|
||||||
|
@ip = a.zero? ? b : @ip + 3
|
||||||
|
end
|
||||||
|
|
||||||
|
def less_than!
|
||||||
|
mode = modes(@mem[@ip])
|
||||||
|
a = @mem.read(@ip + 1, mode[0])
|
||||||
|
b = @mem.read(@ip + 2, mode[1])
|
||||||
|
val = a < b ? 1 : 0
|
||||||
|
@mem.write(@ip + 3, mode[2], val)
|
||||||
|
@ip += 4
|
||||||
|
end
|
||||||
|
|
||||||
|
def equal!
|
||||||
|
mode = modes(@mem[@ip])
|
||||||
|
a = @mem.read(@ip + 1, mode[0])
|
||||||
|
b = @mem.read(@ip + 2, mode[1])
|
||||||
|
val = a == b ? 1 : 0
|
||||||
|
@mem.write(@ip + 3, mode[2], val)
|
||||||
|
@ip += 4
|
||||||
|
end
|
||||||
|
|
||||||
|
def move_relative_base!
|
||||||
|
mode = modes(@mem[@ip])
|
||||||
|
delta = @mem.read(@ip + 1, mode[0])
|
||||||
|
@mem.relative_base += delta
|
||||||
|
@ip += 2
|
||||||
|
end
|
||||||
|
|
||||||
|
def input
|
||||||
|
if block_given?
|
||||||
|
inp = yield
|
||||||
|
raise "Invalid input: #{inp.inspect}" unless inp.is_a?(Numeric)
|
||||||
|
|
||||||
|
return inp
|
||||||
|
end
|
||||||
|
|
||||||
|
print 'input> '
|
||||||
|
gets.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
def operation(cell)
|
||||||
|
(cell % 100)
|
||||||
|
end
|
||||||
|
|
||||||
|
def modes(cell)
|
||||||
|
m = Hash.new(0)
|
||||||
|
(cell / 100).truncate.digits.each_with_index do |d, i|
|
||||||
|
m[i] = d
|
||||||
|
end
|
||||||
|
m
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
127
2019/day11/part1.rb
Normal file
127
2019/day11/part1.rb
Normal file
|
|
@ -0,0 +1,127 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'set'
|
||||||
|
require_relative 'intcode'
|
||||||
|
|
||||||
|
Point = Struct.new(:x, :y) do
|
||||||
|
def to_s
|
||||||
|
"(#{x}, #{y})"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Hull
|
||||||
|
def initialize
|
||||||
|
@panels = Hash.new { |hash, key| hash[key] = :black }
|
||||||
|
@painted = Set.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def color_at(point)
|
||||||
|
@panels[point]
|
||||||
|
end
|
||||||
|
|
||||||
|
def paint(point, color)
|
||||||
|
@painted << point
|
||||||
|
@panels[point] = color
|
||||||
|
end
|
||||||
|
|
||||||
|
def painted
|
||||||
|
@painted.count
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class DoneExecuting < StandardError; end
|
||||||
|
|
||||||
|
class Robot
|
||||||
|
def initialize(program, hull)
|
||||||
|
@computer = Intcode::Computer.new(program.as_memory) { on_input }
|
||||||
|
@hull = hull
|
||||||
|
@facing = :up
|
||||||
|
@position = Point.new(0, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
def step
|
||||||
|
color_output = nil
|
||||||
|
direction_output = nil
|
||||||
|
|
||||||
|
color_output = tick until color_output
|
||||||
|
direction_output = tick until direction_output
|
||||||
|
|
||||||
|
@hull.paint(@position, color(color_output))
|
||||||
|
@facing = turn(@facing, direction_output)
|
||||||
|
@position = move(@position, @facing)
|
||||||
|
end
|
||||||
|
|
||||||
|
def tick
|
||||||
|
ret = @computer.tick
|
||||||
|
raise DoneExecuting if ret == :done_executing
|
||||||
|
|
||||||
|
ret
|
||||||
|
end
|
||||||
|
|
||||||
|
def on_input
|
||||||
|
color = @hull.color_at(@position)
|
||||||
|
case color
|
||||||
|
when :black
|
||||||
|
0
|
||||||
|
when :white
|
||||||
|
1
|
||||||
|
else
|
||||||
|
raise ArgumentError, "unexpected color: #{color}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def color(code)
|
||||||
|
case code
|
||||||
|
when 0
|
||||||
|
:black
|
||||||
|
when 1
|
||||||
|
:white
|
||||||
|
else
|
||||||
|
raise ArgumentError, "unexpected color code: #{code}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
DIRECTIONS = %i[up right down left].freeze
|
||||||
|
|
||||||
|
def turn(facing, direction_code)
|
||||||
|
idx = DIRECTIONS.index(facing)
|
||||||
|
case direction_code
|
||||||
|
when 0
|
||||||
|
# left 90 degrees (ccw)
|
||||||
|
next_idx = (idx - 1) % DIRECTIONS.length
|
||||||
|
when 1
|
||||||
|
# right 90 degrees (cw)
|
||||||
|
next_idx = (idx + 1) % DIRECTIONS.length
|
||||||
|
else
|
||||||
|
raise ArgumentError, "unexpected direction code: #{direction_code}"
|
||||||
|
end
|
||||||
|
DIRECTIONS.fetch(next_idx)
|
||||||
|
end
|
||||||
|
|
||||||
|
def move(pos, direction)
|
||||||
|
case direction
|
||||||
|
when :up
|
||||||
|
Point.new(pos.x, pos.y - 1)
|
||||||
|
when :down
|
||||||
|
Point.new(pos.x, pos.y + 1)
|
||||||
|
when :left
|
||||||
|
Point.new(pos.x - 1, pos.y)
|
||||||
|
when :right
|
||||||
|
Point.new(pos.x + 1, pos.y)
|
||||||
|
else
|
||||||
|
raise ArgumentError, "unexpected direction: #{direction}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
program = Intcode.read(File.join(__dir__, 'input'))
|
||||||
|
hull = Hull.new
|
||||||
|
robot = Robot.new(program, hull)
|
||||||
|
|
||||||
|
loop do
|
||||||
|
robot.step
|
||||||
|
rescue DoneExecuting
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
puts hull.painted
|
||||||
Loading…
Reference in a new issue