Print some debug stuff
This commit is contained in:
parent
4633a84ae1
commit
3a8e1ba1a9
1 changed files with 13 additions and 2 deletions
15
17/main.rb
15
17/main.rb
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
require "debug"
|
require "debug"
|
||||||
|
DEBUG = true
|
||||||
|
|
||||||
input = (ARGV.first.nil? ? DATA : ARGF)
|
input = (ARGV.first.nil? ? DATA : ARGF)
|
||||||
.readlines(chomp: true)
|
.readlines(chomp: true)
|
||||||
|
@ -28,7 +29,7 @@ OPERANDS = [
|
||||||
]
|
]
|
||||||
|
|
||||||
def execute(registers, program)
|
def execute(registers, program)
|
||||||
stdout = []
|
stdout, commands = Array.new, Array.new
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
while i < program.size
|
while i < program.size
|
||||||
|
@ -43,31 +44,41 @@ def execute(registers, program)
|
||||||
numerator = registers[:A]
|
numerator = registers[:A]
|
||||||
denominator = 2 ** combo_operand
|
denominator = 2 ** combo_operand
|
||||||
registers[:A] = numerator / denominator
|
registers[:A] = numerator / denominator
|
||||||
|
commands << [:adv, numerator, denominator]
|
||||||
when 1
|
when 1
|
||||||
registers[:B] = registers[:B] ^ literal_operand
|
registers[:B] = registers[:B] ^ literal_operand
|
||||||
|
commands << [:bxl, literal_operand]
|
||||||
when 2
|
when 2
|
||||||
registers[:B] = combo_operand % 8
|
registers[:B] = combo_operand % 8
|
||||||
|
commands << [:bst, combo_operand % 8]
|
||||||
when 3
|
when 3
|
||||||
if registers[:A] == 0
|
if registers[:A] == 0
|
||||||
:noop
|
commands << [:noop]
|
||||||
else
|
else
|
||||||
i = literal_operand
|
i = literal_operand
|
||||||
|
commands << [:jnz, literal_operand]
|
||||||
end
|
end
|
||||||
when 4
|
when 4
|
||||||
registers[:B] = registers[:B] ^ registers[:C]
|
registers[:B] = registers[:B] ^ registers[:C]
|
||||||
|
commands << [:bxc, registers[:B], registers[:C]]
|
||||||
when 5
|
when 5
|
||||||
stdout << combo_operand % 8
|
stdout << combo_operand % 8
|
||||||
|
commands << [:out, combo_operand % 8]
|
||||||
when 6
|
when 6
|
||||||
numerator = registers[:A]
|
numerator = registers[:A]
|
||||||
denominator = 2 ** combo_operand
|
denominator = 2 ** combo_operand
|
||||||
registers[:B] = numerator / denominator
|
registers[:B] = numerator / denominator
|
||||||
|
commands << [:bdv, numerator, denominator]
|
||||||
when 7
|
when 7
|
||||||
numerator = registers[:A]
|
numerator = registers[:A]
|
||||||
denominator = 2 ** combo_operand
|
denominator = 2 ** combo_operand
|
||||||
registers[:C] = numerator / denominator
|
registers[:C] = numerator / denominator
|
||||||
|
commands << [:cdv, numerator, denominator]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
p commands if DEBUG
|
||||||
|
|
||||||
[registers, program, stdout.join(",")]
|
[registers, program, stdout.join(",")]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue