Day 7: The Treachery of Whales
Setup
Mix.install([
{:kino, "~> 0.4.1"}
])
Input
input = Kino.Input.textarea("Input")
Part 1
defmodule M do
def fuel_cost(positions, pos) do
positions
|> Enum.map(&abs(&1 - pos))
|> Enum.sum()
end
end
Kino.Input.read(input)
|> String.split(",")
|> Enum.map(&String.to_integer(&1))
|> then(&Enum.map(Enum.min(&1)..Enum.max(&1), fn p -> M.fuel_cost(&1, p) end))
|> Enum.min()
Part 2
defmodule M do
def fuel_cost(positions, pos) do
positions
|> Enum.map(fn p ->
x = abs(p - pos)
(x + 1) * x / 2
end)
|> Enum.sum()
end
end
Kino.Input.read(input)
|> String.split(",")
|> Enum.map(&String.to_integer(&1))
|> then(&Enum.map(Enum.min(&1)..Enum.max(&1), fn p -> M.fuel_cost(&1, p) end))
|> Enum.min()