Created
December 2, 2019 20:06
-
-
Save jsdalton/3bd20bb31c1b44f22d90e636070770ed to your computer and use it in GitHub Desktop.
Day 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def unfold(start, &block) | |
Enumerator.new do |y| | |
next_item = start | |
loop do | |
y << next_item | |
next_item = block.call(next_item) | |
end | |
end | |
end | |
def calculate_fuel(mass) | |
(mass / 3.0).floor - 2 | |
end | |
puts(File | |
.read('day-01.txt') | |
.split("\n") | |
.map(&:to_i) | |
.map(&method(:calculate_fuel)) | |
.map do |mass| | |
unfold(mass, &method(:calculate_fuel)) | |
.take_while(&:positive?) | |
.reduce(:+) | |
end | |
.reduce(:+)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment