diff --git a/17/main.rb b/17/main.rb index da1396c..1b67d4a 100755 --- a/17/main.rb +++ b/17/main.rb @@ -1,6 +1,7 @@ #!/usr/bin/env ruby require "debug" +DEBUG = true input = (ARGV.first.nil? ? DATA : ARGF) .readlines(chomp: true) @@ -28,7 +29,7 @@ OPERANDS = [ ] def execute(registers, program) - stdout = [] + stdout, commands = Array.new, Array.new i = 0 while i < program.size @@ -43,31 +44,41 @@ def execute(registers, program) numerator = registers[:A] denominator = 2 ** combo_operand registers[:A] = numerator / denominator + commands << [:adv, numerator, denominator] when 1 registers[:B] = registers[:B] ^ literal_operand + commands << [:bxl, literal_operand] when 2 registers[:B] = combo_operand % 8 + commands << [:bst, combo_operand % 8] when 3 if registers[:A] == 0 - :noop + commands << [:noop] else i = literal_operand + commands << [:jnz, literal_operand] end when 4 registers[:B] = registers[:B] ^ registers[:C] + commands << [:bxc, registers[:B], registers[:C]] when 5 stdout << combo_operand % 8 + commands << [:out, combo_operand % 8] when 6 numerator = registers[:A] denominator = 2 ** combo_operand registers[:B] = numerator / denominator + commands << [:bdv, numerator, denominator] when 7 numerator = registers[:A] denominator = 2 ** combo_operand registers[:C] = numerator / denominator + commands << [:cdv, numerator, denominator] end end + p commands if DEBUG + [registers, program, stdout.join(",")] end