From b5bfbcff0bad765e1fc332e0644ec33f50313b80 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sat, 9 Dec 2023 10:48:34 -0800 Subject: [PATCH] [2023][ruby][9.x] dry --- 2023/ruby/day_09.rb | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/2023/ruby/day_09.rb b/2023/ruby/day_09.rb index 3461a7b..b07873e 100644 --- a/2023/ruby/day_09.rb +++ b/2023/ruby/day_09.rb @@ -1,26 +1,17 @@ histories = ARGF.readlines.map { _1.scan(/-?\d+/).map(&:to_i) } -# part one -p histories - .map {|history| - diffs = [history] - until diffs.last.all?(&:zero?) - diffs << diffs.last.each_cons(2).map { _2 - _1 } - end +history_diffs = histories.map {|history| + diffs = [history] + until diffs.last.all?(&:zero?) + diffs << diffs.last.each_cons(2).map { _2 - _1 } + end + diffs +} - diffs.sum(&:last) - } - .sum +# part one +p history_diffs.sum { _1.sum(&:last) } # part two -p histories - .map {|history| - diffs = [history] - until diffs.last.all?(&:zero?) - diffs << diffs.last.each_cons(2).map { _2 - _1 } - end - - diffs.map(&:first).reverse.inject { _2 - _1 } - } - .sum - +p history_diffs.sum {|diffs| + diffs.map(&:first).reverse.inject { _2 - _1 } +}