You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
946 B
63 lines
946 B
7 days ago
|
registers, program = DATA.read.split("\n\n")
|
||
|
|
||
|
a, b, c = registers.scan(/\d+/).map(&:to_i)
|
||
|
program = program.scan(/\d+/).map(&:to_i)
|
||
|
|
||
|
combo = ->(operand) {
|
||
|
case operand
|
||
|
when 0, 1, 2, 3 then operand
|
||
|
when 4 then a
|
||
|
when 5 then b
|
||
|
when 6 then c
|
||
|
else
|
||
|
fail
|
||
|
end
|
||
|
}
|
||
|
|
||
|
ip = 0
|
||
|
out = []
|
||
|
while (0...program.size).cover?(ip)
|
||
|
opcode = program.fetch(ip)
|
||
|
operand = program.fetch(ip+1)
|
||
|
case opcode
|
||
|
when 0
|
||
|
a = (a / 2**combo[operand]).to_i
|
||
|
ip += 2
|
||
|
when 1
|
||
|
b = b ^ operand
|
||
|
ip += 2
|
||
|
when 2
|
||
|
b = combo[operand] % 8
|
||
|
ip += 2
|
||
|
when 3
|
||
|
if a.zero?
|
||
|
ip += 2
|
||
|
else
|
||
|
ip = operand
|
||
|
end
|
||
|
when 4
|
||
|
b = b ^ c
|
||
|
ip += 2
|
||
|
when 5
|
||
|
out << combo[operand] % 8
|
||
|
ip += 2
|
||
|
when 6
|
||
|
b = (a / 2**combo[operand]).to_i
|
||
|
ip += 2
|
||
|
when 7
|
||
|
c = (a / 2**combo[operand]).to_i
|
||
|
ip += 2
|
||
|
else
|
||
|
fail
|
||
|
end
|
||
|
end
|
||
|
|
||
|
puts out.join(?,)
|
||
|
|
||
|
__END__
|
||
|
Register A: 729
|
||
|
Register B: 0
|
||
|
Register C: 0
|
||
|
|
||
|
Program: 0,1,5,4,3,0
|