[2024][ruby][17.1]

main
Alpha Chen 6 days ago
parent 18daaa3e49
commit ef8e420215
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -43,20 +43,18 @@ end
pp scores.find {|(yx,_),_| yx == stop }[1] pp scores.find {|(yx,_),_| yx == stop }[1]
__END__ __END__
################# ###############
#...#...#...#..E# #.......#....E#
#.#.#.#.#.#.#.#^# #.#.###.#.###.#
#.#.#.#...#...#^# #.....#.#...#.#
#.#.#.#.###.#.#^# #.###.#####.#.#
#>>v#.#.#.....#^# #.#.#.......#.#
#^#v#.#.#.#####^# #.#.#####.###.#
#^#v..#.#.#>>>>^# #...........#.#
#^#v#####.#^###.# ###.#.#####.#.#
#^#v#..>>>>^#...# #...#.....#.#.#
#^#v###^#####.### #.#.#.###.#.#.#
#^#v#>>^#.....#.# #.....#...#.#.#
#^#v#^#####.###.# #.###.#.#.#.#.#
#^#v#^........#.# #S..#.....#...#
#^#v#^#########.# ###############
#S#>>^..........#
#################

@ -0,0 +1,62 @@
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
Loading…
Cancel
Save