2019: Day 5 Part 1
This commit is contained in:
parent
02849a6af9
commit
608b4078c4
3 changed files with 84 additions and 10 deletions
1
2019/day5/input
Normal file
1
2019/day5/input
Normal file
|
|
@ -0,0 +1 @@
|
|||
3,225,1,225,6,6,1100,1,238,225,104,0,1101,48,82,225,102,59,84,224,1001,224,-944,224,4,224,102,8,223,223,101,6,224,224,1,223,224,223,1101,92,58,224,101,-150,224,224,4,224,102,8,223,223,1001,224,3,224,1,224,223,223,1102,10,89,224,101,-890,224,224,4,224,1002,223,8,223,1001,224,5,224,1,224,223,223,1101,29,16,225,101,23,110,224,1001,224,-95,224,4,224,102,8,223,223,1001,224,3,224,1,223,224,223,1102,75,72,225,1102,51,8,225,1102,26,16,225,1102,8,49,225,1001,122,64,224,1001,224,-113,224,4,224,102,8,223,223,1001,224,3,224,1,224,223,223,1102,55,72,225,1002,174,28,224,101,-896,224,224,4,224,1002,223,8,223,101,4,224,224,1,224,223,223,1102,57,32,225,2,113,117,224,101,-1326,224,224,4,224,102,8,223,223,101,5,224,224,1,223,224,223,1,148,13,224,101,-120,224,224,4,224,1002,223,8,223,101,7,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,8,677,226,224,102,2,223,223,1006,224,329,101,1,223,223,107,677,677,224,1002,223,2,223,1006,224,344,101,1,223,223,8,226,677,224,102,2,223,223,1006,224,359,101,1,223,223,107,226,226,224,102,2,223,223,1005,224,374,1001,223,1,223,1108,677,226,224,1002,223,2,223,1006,224,389,101,1,223,223,107,677,226,224,102,2,223,223,1006,224,404,1001,223,1,223,1107,226,677,224,1002,223,2,223,1006,224,419,1001,223,1,223,108,677,677,224,102,2,223,223,1005,224,434,1001,223,1,223,1008,677,226,224,1002,223,2,223,1006,224,449,1001,223,1,223,7,226,677,224,1002,223,2,223,1006,224,464,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,479,1001,223,1,223,1007,226,226,224,1002,223,2,223,1005,224,494,1001,223,1,223,108,226,226,224,1002,223,2,223,1005,224,509,1001,223,1,223,1007,226,677,224,1002,223,2,223,1006,224,524,101,1,223,223,1107,677,677,224,102,2,223,223,1005,224,539,101,1,223,223,1107,677,226,224,102,2,223,223,1005,224,554,1001,223,1,223,108,677,226,224,1002,223,2,223,1006,224,569,1001,223,1,223,1108,226,677,224,1002,223,2,223,1006,224,584,101,1,223,223,8,677,677,224,1002,223,2,223,1006,224,599,1001,223,1,223,1008,226,226,224,102,2,223,223,1006,224,614,101,1,223,223,7,677,677,224,1002,223,2,223,1006,224,629,101,1,223,223,1008,677,677,224,102,2,223,223,1005,224,644,101,1,223,223,7,677,226,224,1002,223,2,223,1005,224,659,101,1,223,223,1108,226,226,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226
|
||||
|
|
@ -2,34 +2,70 @@
|
|||
|
||||
module Intcode
|
||||
def self.read(filename)
|
||||
Program.new File.read(filename).split(',').map(&:to_i)
|
||||
input = File.read(filename)
|
||||
.gsub(/^#.*$/, '')
|
||||
.gsub('\s', '')
|
||||
Program.new(parse(input))
|
||||
end
|
||||
|
||||
def self.parse(input)
|
||||
input.split(',').map(&:to_i)
|
||||
end
|
||||
|
||||
class Program
|
||||
attr_reader :mem
|
||||
attr_accessor :mem
|
||||
def initialize(mem)
|
||||
@mem = mem
|
||||
@mem = mem.clone
|
||||
|
||||
def @mem.to_s
|
||||
batch_size = 10
|
||||
output = String.new
|
||||
each_slice(batch_size).each_with_index do |batch, idx|
|
||||
prefix = idx * batch_size
|
||||
output << "#{prefix}: #{batch.join(' ')}\n"
|
||||
end
|
||||
output
|
||||
end
|
||||
end
|
||||
|
||||
def run(noun, verb)
|
||||
mem = @mem.clone
|
||||
mem[1] = noun
|
||||
mem[2] = verb
|
||||
def run
|
||||
pc = 0
|
||||
loop do
|
||||
case mem[pc]
|
||||
opcode = operation(mem[pc])
|
||||
mode = modes(mem[pc])
|
||||
case opcode
|
||||
when 1
|
||||
r1 = mem[pc + 1]
|
||||
r2 = mem[pc + 2]
|
||||
r3 = mem[pc + 3]
|
||||
mem[r3] = mem[r1] + mem[r2]
|
||||
a = param(r1, mode[0])
|
||||
b = param(r2, mode[1])
|
||||
raise StandardError, "unexpected mode: #{mode[2]}" if mode[2] != 0
|
||||
|
||||
mem[r3] = a + b
|
||||
pc += 4
|
||||
when 2
|
||||
r1 = mem[pc + 1]
|
||||
r2 = mem[pc + 2]
|
||||
r3 = mem[pc + 3]
|
||||
mem[r3] = mem[r1] * mem[r2]
|
||||
a = param(r1, mode[0])
|
||||
b = param(r2, mode[1])
|
||||
raise StandardError, "unexpected mode: #{mode[2]}" if mode[2] != 0
|
||||
|
||||
mem[r3] = a * b
|
||||
pc += 4
|
||||
when 3
|
||||
r1 = mem[pc + 1]
|
||||
raise StandardError, "unexpected mode: #{mode[0]}" if mode[0] != 0
|
||||
|
||||
mem[r1] = input
|
||||
pc += 2
|
||||
when 4
|
||||
r1 = mem[pc + 1]
|
||||
output = param(r1, mode[0])
|
||||
puts "output: #{output}"
|
||||
|
||||
pc += 2
|
||||
when 99
|
||||
return mem[0]
|
||||
else
|
||||
|
|
@ -37,5 +73,35 @@ module Intcode
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def input
|
||||
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
|
||||
|
||||
def param(val, mode)
|
||||
case mode
|
||||
when 0
|
||||
mem[val]
|
||||
when 1
|
||||
val
|
||||
else
|
||||
raise StandardError, "unexpected mode: #{mode}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
7
2019/day5/part1.rb
Normal file
7
2019/day5/part1.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
|
||||
Loading…
Reference in a new issue