blob: 20bbc66ea51b565f8d05d3d1123d186ee1db027e (
plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#!/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)
noun = 0
verb = 0
loop do
input = Input.dup
input[1] = noun
input[2] = verb
m = Machine.new(input)
m.run
break if m.mem(0, 1)[0] == 19690720
verb += 1
if verb == 100 then
verb = 0
noun += 1
end
end
puts (100 * noun) + verb
|