#!/usr/bin/env ruby class Machine def initialize(starting_memory = []) @mem = starting_memory @pc = 0 @halt = false end def mem(start = 0, len = @mem.length) @mem[start...(start + len)] end def halt! @halt = true end def halt? @halt end def step return if halt? case @mem[@pc] when 1 @mem[@mem[@pc+3]] = @mem[@mem[@pc+1]] + @mem[@mem[@pc+2]] @pc += 4 when 2 @mem[@mem[@pc+3]] = @mem[@mem[@pc+1]] * @mem[@mem[@pc+2]] @pc += 4 when 99 halt! @pc += 1 end end def run until halt? do step end end end input = $stdin.readlines[0].strip.split(",").map(&:to_i) input[1] = 12 input[2] = 2 m = Machine.new(input) m.run puts m.mem(0, 1)