Some stuff
This commit is contained in:
parent
6164e1edee
commit
18ed85df4b
1 changed files with 34 additions and 23 deletions
57
17/main.rb
57
17/main.rb
|
@ -20,22 +20,21 @@ program = input[split+1]
|
||||||
.split(",")
|
.split(",")
|
||||||
.map(&:to_i)
|
.map(&:to_i)
|
||||||
|
|
||||||
OPERANDS = [
|
|
||||||
->() { 0 }, ->() { 1 }, ->() { 2 }, ->() { 3 },
|
|
||||||
->() { registers[:A] },
|
|
||||||
->() { registers[:B] },
|
|
||||||
->() { registers[:C] },
|
|
||||||
->() { :noop },
|
|
||||||
]
|
|
||||||
|
|
||||||
def execute(registers, program)
|
def execute(registers, program)
|
||||||
stdout, commands = Array.new, Array.new
|
stdout, commands = Array.new, Array.new
|
||||||
|
operands = [
|
||||||
|
->() { 0 }, ->() { 1 }, ->() { 2 }, ->() { 3 },
|
||||||
|
->() { registers[:A] },
|
||||||
|
->() { registers[:B] },
|
||||||
|
->() { registers[:C] },
|
||||||
|
->() { :noop },
|
||||||
|
]
|
||||||
|
|
||||||
ic = 0
|
ic = 0
|
||||||
while ic < program.size
|
while ic < program.size
|
||||||
opcode = program[ic]
|
opcode = program[ic]
|
||||||
literal_operand = program[ic+1]
|
literal_operand = program[ic+1]
|
||||||
combo_operand = OPERANDS[literal_operand].call
|
combo_operand = operands[literal_operand].call
|
||||||
|
|
||||||
ic += 2
|
ic += 2
|
||||||
|
|
||||||
|
@ -84,25 +83,37 @@ def execute(registers, program)
|
||||||
[registers, program, stdout.join(",")]
|
[registers, program, stdout.join(",")]
|
||||||
end
|
end
|
||||||
|
|
||||||
part_2 = registers[:A].dup
|
_, _, part_1 = execute(registers.dup, program)
|
||||||
_, _, part_1 = execute(registers, program)
|
|
||||||
p part_1
|
p part_1
|
||||||
|
|
||||||
while true
|
|
||||||
part_2 += 100_00
|
def reverse_engineer(program, slot, i)
|
||||||
registers = {A: part_2, B: 0, C: 0}
|
candidates = (0..8).map do |i|
|
||||||
_, _, output = execute(registers, program)
|
[i, execute({A: i, B: 0, C: 0}, program).last.split(",").map(&:to_i)
|
||||||
p "#{part_2} (a): #{output}"
|
]
|
||||||
break if output.match?(/4,1,5,5,0,3,3,0$/)
|
end.select { _1.last == program[slot..] }
|
||||||
end
|
end
|
||||||
|
|
||||||
while part_2 <<= 3
|
part_2 = 0
|
||||||
registers = {A: part_2, B: 0, C: 0}
|
(program.size - 1).downto(0) do |slot|
|
||||||
_, _, output = execute(registers, program)
|
candidates = []
|
||||||
|
(0..8).each do |i|
|
||||||
|
candidate = part_2 + i
|
||||||
|
_, _, output = execute({A: candidate, B: 0, C: 0}, program)
|
||||||
|
new_program = output.split(",").map(&:to_i)
|
||||||
|
|
||||||
next if output.size < 31
|
if new_program == program[slot..]
|
||||||
p "#{part_2} (b): #{output}"
|
candidates << candidate
|
||||||
raise if output.size > 31
|
unless [301,2566855701950, 20534845615581, 20534845615573, 20534845615583].include?(candidate)
|
||||||
|
part_2 = candidate
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
#p candidates
|
||||||
|
if slot == 0
|
||||||
|
p candidates.first
|
||||||
|
end
|
||||||
|
part_2 <<= 3
|
||||||
end
|
end
|
||||||
|
|
||||||
__END__
|
__END__
|
||||||
|
|
Loading…
Reference in a new issue