2019: Day 2 Part 1
This commit is contained in:
parent
998c4a13a9
commit
57bfcebc3e
2 changed files with 30 additions and 0 deletions
29
2019/day2/part1.rb
Normal file
29
2019/day2/part1.rb
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
program = File.read(File.join(__dir__, 'input'))
|
||||
|
||||
def run(mem)
|
||||
pc = 0
|
||||
loop do
|
||||
case mem[pc]
|
||||
when 1
|
||||
r1, r2, r3 = mem[pc+1], mem[pc+2], mem[pc+3]
|
||||
mem[r3] = mem[r1] + mem[r2]
|
||||
pc += 4
|
||||
when 2
|
||||
r1, r2, r3 = mem[pc+1], mem[pc+2], mem[pc+3]
|
||||
mem[r3] = mem[r1] * mem[r2]
|
||||
pc += 4
|
||||
when 99
|
||||
return
|
||||
else
|
||||
raise StandardError, "unexpected opcode: #{opcode}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
mem = program.split(',').map(&:to_i)
|
||||
mem[1] = 12
|
||||
mem[2] = 2
|
||||
run(mem)
|
||||
puts mem[0]
|
||||
Loading…
Reference in a new issue