AoC19/lib/day1/day1.ex

36 lines
750 B
Elixir
Raw Permalink Normal View History

2019-12-01 05:26:57 +00:00
defmodule Day1 do
def part1 do
File.read!("lib/day1/input.txt")
|> String.split("\n")
|> Enum.reject(&(&1 == ""))
|> Enum.map(fn line ->
{module, _} = Integer.parse(line)
fuel_for(module)
end)
|> Enum.sum()
end
def part2 do
File.read!("lib/day1/input.txt")
|> String.split("\n")
|> Enum.reject(&(&1 == ""))
|> Enum.map(fn line ->
{module, _} = Integer.parse(line)
total_fuel(module)
end)
|> Enum.sum()
end
def total_fuel(mass \\ part1())
def total_fuel(mass) when mass <= 0, do: mass
def total_fuel(mass) do
fuel_mass = fuel_for(mass)
fuel_mass + total_fuel(fuel_mass)
end
def fuel_for(mass) do
max(Integer.floor_div(mass, 3) - 2, 0)
end
end