Watch
1
0
Fork
You've already forked advent-of-code
0

2019: Day 2 Part 1

This commit is contained in:
Jeremy Kaplan 2019-12-01 22:20:41 -08:00
commit 57bfcebc3e
2 changed files with 30 additions and 0 deletions

1
2019/day2/input Normal file
View file

@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,10,1,19,2,9,19,23,2,13,23,27,1,6,27,31,2,6,31,35,2,13,35,39,1,39,10,43,2,43,13,47,1,9,47,51,1,51,13,55,1,55,13,59,2,59,13,63,1,63,6,67,2,6,67,71,1,5,71,75,2,6,75,79,1,5,79,83,2,83,6,87,1,5,87,91,1,6,91,95,2,95,6,99,1,5,99,103,1,6,103,107,1,107,2,111,1,111,5,0,99,2,14,0,0

29
2019/day2/part1.rb Normal file
View 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]