aboutsummaryrefslogtreecommitdiff
path: root/day02/part1
blob: da8226b692c94df21a2fe9b17cc723a9cc1b147c (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
#!/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)