1
0
Fork 0

Some stuff

This commit is contained in:
Andrew Tomaka 2024-12-17 12:05:03 -05:00
parent 3a8e1ba1a9
commit 6164e1edee
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE

View file

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