part 1 working and prep part 2
This commit is contained in:
parent
19147d460c
commit
4633a84ae1
1 changed files with 47 additions and 37 deletions
84
17/main.rb
84
17/main.rb
|
@ -27,53 +27,63 @@ OPERANDS = [
|
|||
->() { :noop },
|
||||
]
|
||||
|
||||
stdout = []
|
||||
def execute(registers, program)
|
||||
stdout = []
|
||||
|
||||
i = 0
|
||||
while i < program.size
|
||||
opcode = program[i]
|
||||
literal_operand = program[i+1]
|
||||
combo_operand = OPERANDS[literal_operand].call
|
||||
i = 0
|
||||
while i < program.size
|
||||
opcode = program[i]
|
||||
literal_operand = program[i+1]
|
||||
combo_operand = OPERANDS[literal_operand].call
|
||||
|
||||
i += 2
|
||||
i += 2
|
||||
|
||||
case opcode
|
||||
when 0
|
||||
numerator = registers[:A]
|
||||
denominator = 2 ** combo_operand
|
||||
registers[:A] = numerator / denominator
|
||||
when 1
|
||||
registers[:B] = registers[:B] ^ literal_operand
|
||||
when 2
|
||||
registers[:B] = combo_operand % 8
|
||||
when 3
|
||||
if registers[:A] == 0
|
||||
:noop
|
||||
else
|
||||
i = literal_operand
|
||||
case opcode
|
||||
when 0
|
||||
numerator = registers[:A]
|
||||
denominator = 2 ** combo_operand
|
||||
registers[:A] = numerator / denominator
|
||||
when 1
|
||||
registers[:B] = registers[:B] ^ literal_operand
|
||||
when 2
|
||||
registers[:B] = combo_operand % 8
|
||||
when 3
|
||||
if registers[:A] == 0
|
||||
:noop
|
||||
else
|
||||
i = literal_operand
|
||||
end
|
||||
when 4
|
||||
registers[:B] = registers[:B] ^ registers[:C]
|
||||
when 5
|
||||
stdout << combo_operand % 8
|
||||
when 6
|
||||
numerator = registers[:A]
|
||||
denominator = 2 ** combo_operand
|
||||
registers[:B] = numerator / denominator
|
||||
when 7
|
||||
numerator = registers[:A]
|
||||
denominator = 2 ** combo_operand
|
||||
registers[:C] = numerator / denominator
|
||||
end
|
||||
when 4
|
||||
registers[:B] = registers[:B] ^ registers[:C]
|
||||
when 5
|
||||
stdout << combo_operand % 8
|
||||
when 6
|
||||
numerator = registers[:A]
|
||||
denominator = 2 ** combo_operand
|
||||
registers[:B] = numerator / denominator
|
||||
when 7
|
||||
numerator = registers[:A]
|
||||
denominator = 2 ** combo_operand
|
||||
registers[:C] = numerator / denominator
|
||||
end
|
||||
|
||||
[registers, program, stdout.join(",")]
|
||||
end
|
||||
|
||||
p registers
|
||||
original_registers = registers.dup
|
||||
_, _, part_1 = execute(registers, program)
|
||||
p part_1
|
||||
|
||||
puts stdout.join(",")
|
||||
registers = original_registers.dup
|
||||
_, _, part_2 = execute(registers, program)
|
||||
p program.join(",")
|
||||
p part_2
|
||||
p part_2 == program.join(",") ? "Correct!" : "Not correct!"
|
||||
|
||||
__END__
|
||||
Register A: 729
|
||||
Register A: 2024
|
||||
Register B: 0
|
||||
Register C: 0
|
||||
|
||||
Program: 0,1,5,4,3,0
|
||||
Program: 0,3,5,4,3,0
|
||||
|
|
Loading…
Reference in a new issue