From b15f5ee2a1fbe80ad6a8dde631d11f28794849b0 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sun, 1 Dec 2019 20:40:52 -0800 Subject: [PATCH] [2019][ruby][1.2] --- 2019/ruby/Gemfile | 7 +++++++ 2019/ruby/Gemfile.lock | 13 +++++++++++++ 2019/ruby/day_01.rb | 15 +++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 2019/ruby/Gemfile create mode 100644 2019/ruby/Gemfile.lock create mode 100644 2019/ruby/day_01.rb diff --git a/2019/ruby/Gemfile b/2019/ruby/Gemfile new file mode 100644 index 0000000..e03b97c --- /dev/null +++ b/2019/ruby/Gemfile @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } + +gem "minitest" diff --git a/2019/ruby/Gemfile.lock b/2019/ruby/Gemfile.lock new file mode 100644 index 0000000..5846597 --- /dev/null +++ b/2019/ruby/Gemfile.lock @@ -0,0 +1,13 @@ +GEM + remote: https://rubygems.org/ + specs: + minitest (5.13.0) + +PLATFORMS + ruby + +DEPENDENCIES + minitest + +BUNDLED WITH + 2.0.2 diff --git a/2019/ruby/day_01.rb b/2019/ruby/day_01.rb new file mode 100644 index 0000000..c766399 --- /dev/null +++ b/2019/ruby/day_01.rb @@ -0,0 +1,15 @@ +# puts ARGF.read.lines.map(&:to_i).map {|mass| +# (mass / 3.0).floor - 2 +# }.sum + +fuel = ->(mass) { + total = 0 + loop do + mass = (mass / 3.0).floor - 2 + break if mass <= 0 + total += mass + end + total +} + +puts ARGF.read.lines.map(&:to_i).map(&fuel).sum