diff --git a/17/main.rb b/17/main.rb index 1b67d4a..2068955 100755 --- a/17/main.rb +++ b/17/main.rb @@ -1,7 +1,7 @@ #!/usr/bin/env ruby require "debug" -DEBUG = true +DEBUG = false input = (ARGV.first.nil? ? DATA : ARGF) .readlines(chomp: true) @@ -31,13 +31,13 @@ OPERANDS = [ def execute(registers, program) stdout, commands = Array.new, Array.new - i = 0 - while i < program.size - opcode = program[i] - literal_operand = program[i+1] + ic = 0 + while ic < program.size + opcode = program[ic] + literal_operand = program[ic+1] combo_operand = OPERANDS[literal_operand].call - i += 2 + ic += 2 case opcode when 0 @@ -55,7 +55,7 @@ def execute(registers, program) if registers[:A] == 0 commands << [:noop] else - i = literal_operand + ic = literal_operand commands << [:jnz, literal_operand] end when 4 @@ -77,20 +77,33 @@ def execute(registers, program) end end - p commands if DEBUG + if DEBUG + commands.each { puts _1.join(" ") } + end [registers, program, stdout.join(",")] end -original_registers = registers.dup +part_2 = registers[:A].dup _, _, part_1 = execute(registers, program) p part_1 -registers = original_registers.dup -_, _, part_2 = execute(registers, program) -p program.join(",") -p part_2 -p part_2 == program.join(",") ? "Correct!" : "Not correct!" +while true + part_2 += 100_00 + registers = {A: part_2, B: 0, C: 0} + _, _, output = execute(registers, program) + p "#{part_2} (a): #{output}" + break if output.match?(/4,1,5,5,0,3,3,0$/) +end + +while part_2 <<= 3 + registers = {A: part_2, B: 0, C: 0} + _, _, output = execute(registers, program) + + next if output.size < 31 + p "#{part_2} (b): #{output}" + raise if output.size > 31 +end __END__ Register A: 2024