[2021][ruby][11.x] tweak loop

pull/2/head
Alpha Chen 3 years ago
parent 212ef6a381
commit d29ae63a86

@ -15,9 +15,7 @@ neighbors = ->(y, x) {
# 100.times do # 100.times do
(1..).each do |i| (1..).each do |i|
octopuses.transform_values! { _1 + 1 } octopuses.transform_values! { _1 + 1 }
loop do until (flashing = octopuses.select { _2 == 10 }).empty?
flashing = octopuses.select { _2 == 10 }
break if flashing.empty?
flashing.each do |(y,x),_| flashing.each do |(y,x),_|
octopuses[[y,x]] += 1 octopuses[[y,x]] += 1
neighbors.(y,x) neighbors.(y,x)
@ -27,7 +25,10 @@ neighbors = ->(y, x) {
end end
end end
end end
puts i or exit if octopuses.values.all? { _1 > 9 } if octopuses.values.all? { _1 > 9 }
puts i
exit
end
# flashes += octopuses.count { _2 > 9 } # flashes += octopuses.count { _2 > 9 }
octopuses.transform_values! { _1 > 10 ? 0 : _1 } octopuses.transform_values! { _1 > 10 ? 0 : _1 }
end end

Loading…
Cancel
Save