Watch
1
0
Fork
You've already forked advent-of-code
0

2019: Day 1 Part 2

This commit is contained in:
Jeremy Kaplan 2019-12-01 12:40:55 -08:00
commit b5133271c7

24
2019/day1/part2.rb Normal file
View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
def fuel_needed(mass)
[(mass / 3).truncate - 2, 0].max
end
def total_fuel(mass)
fuel = 0
current = mass
while current.positive?
current = fuel_needed(current)
fuel += current
end
fuel
end
fuel = 0
File.open(File.join(__dir__, 'input')) do |file|
file.readlines.each do |line|
mass = line.to_i
fuel += total_fuel(mass)
end
end
puts fuel