From ea42e4bbc854cd833d5d3394755e80b88594c462 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Wed, 1 Dec 2021 21:06:02 -0800 Subject: [PATCH] [2021][ruby][2.x] --- 2021/ruby/day_02.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 2021/ruby/day_02.rb diff --git a/2021/ruby/day_02.rb b/2021/ruby/day_02.rb new file mode 100644 index 0000000..7e957f3 --- /dev/null +++ b/2021/ruby/day_02.rb @@ -0,0 +1,35 @@ +# h = 0 +# d = 0 +# ARGF.read.split("\n").map(&:split).each do |dir, n| +# n = n.to_i +# case dir +# when "forward" +# h += n +# when "down" +# d += n +# when "up" +# d -= n +# else +# fail dir +# end +# end +# p h, d, h*d + +h = 0 +d = 0 +aim = 0 +ARGF.read.split("\n").map(&:split).each do |dir, n| + n = n.to_i + case dir + when "forward" + h += n + d += aim * n + when "down" + aim += n + when "up" + aim -= n + else + fail dir + end +end +p h, d, h*d