Skip to content

Instantly share code, notes, and snippets.

@jsdalton
Created December 2, 2019 20:06
Show Gist options
  • Save jsdalton/3bd20bb31c1b44f22d90e636070770ed to your computer and use it in GitHub Desktop.
Save jsdalton/3bd20bb31c1b44f22d90e636070770ed to your computer and use it in GitHub Desktop.
Day 2
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