DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 1: The Tyranny of the Rocket Equation

Collapse
 
yordiverkroost profile image
Yordi Verkroost

Day 1, always good to start nice and easy!

Solution for part two:

defmodule Aoc19.Day1b do
  @moduledoc false

  alias Aoc19.Utils.Common
  alias Aoc19.Utils.Day1, as: Day1Utils

  def start(input_location) do
    input_location
    |> Common.read_numbers()
    |> Enum.reduce(0, &fuel/2)
    |> trunc()
  end

  def fuel(0, total), do: total

  def fuel(mass, total) do
    fuel =
      mass
      |> Day1Utils.fuel()
      |> Kernel.max(0)

    fuel(fuel, total + fuel)
  end
end

And here is part one