From 4d7ad2055f8cc548030c254c50c6379e01731d66 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sun, 12 Dec 2021 21:20:19 -0800 Subject: [PATCH] [2021][ruby][13.x] refactor --- 2021/ruby/day_13.rb | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/2021/ruby/day_13.rb b/2021/ruby/day_13.rb index 169fd43..d9f3f74 100644 --- a/2021/ruby/day_13.rb +++ b/2021/ruby/day_13.rb @@ -4,17 +4,13 @@ dots = dots.scan(/(\d+),(\d+)/).map { _1.map(&:to_i) } folds = folds.scan(/(x|y)=(\d+)/).map { [_1, _2.to_i] } folds.each do |dir, axis| - case dir - when ?x - dots.select {|x,y| x > axis }.each do |dot| - dot[0] = axis - (dot[0] - axis) - end - when ?y - dots.select {|x,y| y > axis }.each do |dot| - dot[1] = axis - (dot[1] - axis) - end - else - fail + index = case dir + when ?x then 0 + when ?y then 1 + else fail + end + dots.select { _1[index] > axis }.each do |dot| + dot[index] = axis - (dot[index] - axis) end dots.uniq! # p dots.size or exit