Day 7
Setup
Mix.install([
{:kino, "~> 0.4.1"}
])
input = Kino.Input.textarea("Problem input")
parsed_input =
input
|> Kino.Input.read()
|> String.split(",")
|> Enum.map(&String.to_integer/1)
parsed_input
|> Enum.min_max()
Part 1
freq =
parsed_input
|> Enum.frequencies()
possible_solutions =
freq
|> Map.keys()
|> Enum.min_max()
|> then(fn {min, max} -> Range.new(min, max) end)
Enum.reduce(possible_solutions, :infinity, fn sol, best ->
fuel_spent =
Enum.map(freq, fn {position, crabs} -> abs(position - sol) * crabs end)
|> Enum.sum()
if fuel_spent < best do
fuel_spent
else
best
end
end)
Part 2
Enum.reduce(possible_solutions, :infinity, fn sol, best ->
fuel_spent =
Enum.map(freq, fn {position, crabs} -> (1..abs(position - sol) |> Enum.sum()) * crabs end)
|> Enum.sum()
if fuel_spent < best do
fuel_spent
else
best
end
end)