aboutsummaryrefslogtreecommitdiff
path: root/intcode/das
blob: c84d9a04bbf9d245243c8c11cc45fc9da8c865ee (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
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env ruby

class Program
  def initialize(program)
    @mem = program
    @pc = 0
  end

  def dis_next
    opcode = @mem[@pc] % 100

    p3, p2, p1 = ("%03d" % (@mem[@pc] / 100)).chars

    print "#{@pc}: "
    case opcode
    when 1
      puts "add #{Program.sigil(p1)}#{@mem[@pc+1]}, #{Program.sigil(p2)}#{@mem[@pc+2]}, #{Program.sigil(p3)}#{@mem[@pc+3]}"
      @pc += 4
    when 2
      puts "mul #{Program.sigil(p1)}#{@mem[@pc+1]}, #{Program.sigil(p2)}#{@mem[@pc+2]}, #{Program.sigil(p3)}#{@mem[@pc+3]}"
      @pc += 4
    when 3
      puts "inp #{Program.sigil(p1)}#{@mem[@pc+1]}"
      @pc += 2
    when 4
      puts "out #{Program.sigil(p1)}#{@mem[@pc+1]}"
      @pc += 2
    when 5
      puts "jnz #{Program.sigil(p1)}#{@mem[@pc+1]}, #{Program.sigil(p2)}#{@mem[@pc+2]}"
      @pc += 3
    when 6
      puts "jez #{Program.sigil(p1)}#{@mem[@pc+1]}, #{Program.sigil(p2)}#{@mem[@pc+2]}"
      @pc += 3
    when 7
      puts "tlt #{Program.sigil(p1)}#{@mem[@pc+1]}, #{Program.sigil(p2)}#{@mem[@pc+2]}, #{Program.sigil(p3)}#{@mem[@pc+3]}"
      @pc += 4
    when 8
      puts "teq #{Program.sigil(p1)}#{@mem[@pc+1]}, #{Program.sigil(p2)}#{@mem[@pc+2]}, #{Program.sigil(p3)}#{@mem[@pc+3]}"
      @pc += 4
    when 9
      puts "arb #{Program.sigil(p1)}#{@mem[@pc+1]}"
      @pc += 2
    when 99
      puts "hlt"
      @pc += 1
    else
      puts "@dat #{@mem[@pc]}"
      @pc += 1
    end
  end

  def dis_all
    until @pc >= @mem.length do
      dis_next
    end
  end

  private

  def self.sigil(p)
    case p
    when "0"
      "@"
    when "2"
      "&"
    end
  end
end

exit 1 if ARGV.length != 1

source = ARGV.shift.strip

input = File.readlines(source)[0].strip.split(",").map(&:to_i)

p = Program.new(input)
p.dis_all