diff options
Diffstat (limited to 'day09/das')
-rwxr-xr-x | day09/das | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/day09/das b/day09/das new file mode 100755 index 0000000..9b1eecf --- /dev/null +++ b/day09/das @@ -0,0 +1,73 @@ +#!/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 "in #{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 + +input = $stdin.readlines[0].strip.split(",").map(&:to_i) + +p = Program.new(input) +p.dis_all |