A super gentle start to the festive season with a problem that is, essentially, apply a simple function to a short list of inputs and find the sum. It was almost more complicated to convert the input file into a usable format:
inputs = map(int, input.split())
Once we have a list of integer values, the function is a piece of cake, especially when the "rounding down" requirement after division is a freebie:
def fuel(n): return n / 3 - 2 print(sum([fuel(i) for i in inputs]))
The second start adds in a small twist: the values we've just calculated also requires fuel, and their fuel requires fuel, until we reach negligible values. Clearly we're being asked to get recursive:
def fuel2(n): extra = fuel(n) if extra < 1: return 0 else: return extra + fuel2(extra)
And that's it, first 4% of AOC2019 in the bag (or Santa's sack)!
ETA: Just for fun, let's do that in Elixir too.
defmodule Day1 do defmacro is_free(mass) do quote do: (div(unquote(mass), 3) - 2) < 1 end def fuel(mass), do: div(mass, 3) - 2 def recursive_fuel(mass) when is_free(mass), do: 0 def recursive_fuel(mass), do: fuel(mass) + recursive_fuel(fuel(mass)) end