-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_intcode.rb
More file actions
32 lines (24 loc) · 792 Bytes
/
02_intcode.rb
File metadata and controls
32 lines (24 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require_relative 'lib/intcode'
def run(mem, noun, verb)
mem = mem.dup
mem[1] = noun
mem[2] = verb
Intcode.new(mem, valid_ops: [1, 2, 99]).then(&:continue).memory[0]
end
input = (ARGV[0]&.include?(?,) ? ARGV[0] : ARGF.read).split(?,).map(&method(:Integer)).freeze
puts run(input, 12, 2)
# Note that for known Advent of Code inputs,
# mem[0] = noun * N + verb * V + base
# And V = 1, but I'll allow for others.
base = run(input, 0, 0)
delta_noun = run(input, 1, 0) - base
delta_verb = run(input, 0, 1) - base
target = 19690720
if delta_noun > delta_verb
noun = (target - base) / delta_noun
verb = (target - base - delta_noun * noun) / delta_verb
else
verb = (target - base) / delta_verb
noun = (target - base - delta_verb * verb) / delta_noun
end
puts noun * 100 + verb