From 4179982a821317e69571819f52700de143d3737b Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Wed, 4 Dec 2019 21:37:42 -0800 Subject: [PATCH] [2019][ruby][5.x] --- 2019/ruby/day_05.rb | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 2019/ruby/day_05.rb diff --git a/2019/ruby/day_05.rb b/2019/ruby/day_05.rb new file mode 100644 index 0000000..b6681a9 --- /dev/null +++ b/2019/ruby/day_05.rb @@ -0,0 +1,52 @@ +OPCODES = ARGF.read.split(?,).map(&:to_i) + +def run(memory) + pc = 0 + loop do + instruction = memory[pc] + return if instruction == 99 + + p pc + a, b, c, opcode = instruction.to_s.rjust(5, ?0).scan(/^(0|1)(0|1)(0|1)(\d\d)$/)[0].map(&:to_i) + c = c.zero? ? memory[memory[pc+1]] : memory[pc+1] + b = b.zero? ? memory[memory[pc+2]] : memory[pc+2] + a = a.zero? ? memory[memory[pc+3]] : memory[pc+3] + + case opcode + when 1 + memory[memory[pc+3]] = c + b + pc += 4 + when 2 + memory[memory[pc+3]] = c * b + pc += 4 + when 3 + memory[memory[pc+1]] = 5 + pc += 2 + when 4 + puts c + pc += 2 + when 5 + if c.nonzero? + pc = b + else + pc += 3 + end + when 6 + if c.zero? + pc = b + else + pc += 3 + end + when 7 + memory[memory[pc+3]] = (c < b) ? 1 : 0 + pc += 4 + when 8 + memory[memory[pc+3]] = (c == b) ? 1 : 0 + pc += 4 + else + fail "unrecognized instruction: #{instruction}" + end + end +end + +run(OPCODES)