diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-01-27 15:00:57 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-01-27 15:00:57 +0100 |
commit | 29c49417f89169c03ca05724da3c88880d3a1a17 (patch) | |
tree | a2105c4a8e1da21b617035dda3d695560578e0d7 /aoc-2019-elixir/lib/day01.ex | |
parent | 6c747854c00b5f96038a726a90a7201fefd626bb (diff) | |
download | gleam_aoc2020-29c49417f89169c03ca05724da3c88880d3a1a17.tar.gz gleam_aoc2020-29c49417f89169c03ca05724da3c88880d3a1a17.zip |
Solve first two days of 2019 in Elixir
Diffstat (limited to 'aoc-2019-elixir/lib/day01.ex')
-rw-r--r-- | aoc-2019-elixir/lib/day01.ex | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/aoc-2019-elixir/lib/day01.ex b/aoc-2019-elixir/lib/day01.ex new file mode 100644 index 0000000..6545f19 --- /dev/null +++ b/aoc-2019-elixir/lib/day01.ex @@ -0,0 +1,32 @@ +defmodule Day01 do + @spec part1([integer]) :: integer + def part1(input) do + input + |> Enum.map(&naive_fuel_requirement/1) + |> Enum.sum() + end + + @spec part2([integer]) :: integer + def part2(input) do + input + |> Enum.map(&recursive_fuel_requirement/1) + |> Enum.sum() + end + + defp naive_fuel_requirement(mass) do + div(mass, 3) - 2 + end + + defp recursive_fuel_requirement(mass) do + fuel_mass = div(mass, 3) - 2 + + case fuel_mass do + n when n <= 0 -> 0 + _ -> fuel_mass + recursive_fuel_requirement(fuel_mass) + end + end +end + +input = Util.Input.read_numbers("day01") +input |> Day01.part1() |> IO.inspect() +input |> Day01.part2() |> IO.inspect() |