From d29ae63a8690de59a323bd4fb894d414b1c34613 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sat, 11 Dec 2021 09:56:12 -0800 Subject: [PATCH] [2021][ruby][11.x] tweak loop --- 2021/ruby/day_11.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/2021/ruby/day_11.rb b/2021/ruby/day_11.rb index 236c789..a482740 100644 --- a/2021/ruby/day_11.rb +++ b/2021/ruby/day_11.rb @@ -15,9 +15,7 @@ neighbors = ->(y, x) { # 100.times do (1..).each do |i| octopuses.transform_values! { _1 + 1 } - loop do - flashing = octopuses.select { _2 == 10 } - break if flashing.empty? + until (flashing = octopuses.select { _2 == 10 }).empty? flashing.each do |(y,x),_| octopuses[[y,x]] += 1 neighbors.(y,x) @@ -27,7 +25,10 @@ neighbors = ->(y, x) { 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 } octopuses.transform_values! { _1 > 10 ? 0 : _1 } end