26 lines
473 B
Ruby
Executable File
26 lines
473 B
Ruby
Executable File
#!/usr/bin/env ruby
|
|
|
|
INPUT = __dir__ + '/input'
|
|
|
|
def part1
|
|
File.readlines(INPUT).map { |mass| calculate_fuel(mass) }.inject(:+)
|
|
end
|
|
|
|
def part2
|
|
File.readlines(INPUT).map { |mass| calculate_total_fuel(mass.to_i) }.inject(:+)
|
|
end
|
|
|
|
def calculate_total_fuel(mass)
|
|
fuel = calculate_fuel(mass)
|
|
fuel > 0 ? fuel + calculate_total_fuel(fuel) : 0
|
|
end
|
|
|
|
def calculate_fuel(mass)
|
|
(mass.to_i / 3).floor - 2
|
|
end
|
|
|
|
puts "=== Part 1 ==="
|
|
puts part1
|
|
puts "=== Part 2 ==="
|
|
puts part2
|