From 4dd5e8d92047289b6f3a10f0cfc4aed81d25496f Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Wed, 2 Dec 2020 21:24:36 -0800 Subject: [PATCH] [2020][ruby][3.x] --- 2020/ruby/day_03.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 2020/ruby/day_03.rb diff --git a/2020/ruby/day_03.rb b/2020/ruby/day_03.rb new file mode 100644 index 0000000..abf4912 --- /dev/null +++ b/2020/ruby/day_03.rb @@ -0,0 +1,18 @@ +map = ARGF.read.split("\n").map(&:chars) +slopes = [ + [1, 1], + [3, 1], + [5, 1], + [7, 1], + [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 +}.reduce(&:*)