Watch
1
0
Fork
You've already forked advent-of-code
0
advent-of-code/2019/day1/part2.rb
2021-12-04 21:20:03 -08:00

24 lines
401 B
Ruby

# 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