2019: Day 1 Part 2
This commit is contained in:
parent
8878253700
commit
b5133271c7
1 changed files with 24 additions and 0 deletions
24
2019/day1/part2.rb
Normal file
24
2019/day1/part2.rb
Normal 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
|
||||||
Loading…
Reference in a new issue