From ea6d3fed3b43ef819ae2294470be67f9b952b5b1 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Wed, 2 Dec 2020 21:24:48 -0800 Subject: [PATCH] [2020][ruby][3.x] too clever --- 2020/ruby/day_03.rb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/2020/ruby/day_03.rb b/2020/ruby/day_03.rb index abf4912..9e762a7 100644 --- a/2020/ruby/day_03.rb +++ b/2020/ruby/day_03.rb @@ -1,4 +1,6 @@ map = ARGF.read.split("\n").map(&:chars) +rows = map.size +cols = map[0].size slopes = [ [1, 1], [3, 1], @@ -7,12 +9,7 @@ slopes = [ [1, 2], ] puts slopes.map {|dx,dy| - x, y = 0, 0 - trees = 0 - until y >= map.length - trees += 1 if map[y][x % map[0].size] == ?# - x += dx - y += dy - end - trees + map.each_slice(dy).map(&:first) + .map.with_index {|row,i| row[dx*i % cols] } + .count(?#) }.reduce(&:*)