Red-Nosed Reports
Mix.install([{:kino, "~> 0.14.2"}])
Input
input = Kino.Input.textarea("Input:")
input = Kino.Input.read(input)
reports =
input
|> String.trim()
|> String.split("\n")
|> Enum.map(fn x -> String.split(x) |> Enum.map(&String.to_integer/1) end)
classify = fn report ->
pairs = report |> Enum.chunk_every(2, 1, :discard)
directions =
Enum.map(pairs, fn
[a, b] when a > b and a <= b + 3 -> :decrease
[a, b] when a < b and b <= a + 3 -> :increase
_ -> :error
end)
Enum.reduce(directions, fn
:increase, :increase -> :increase
:decrease, :decrease -> :decrease
_, _ -> :error
end)
end
classify.([7, 6, 4, 2, 1])
is_safe = fn report -> classify.(report) != :error end
Part 1
Enum.count(reports, is_safe)
Part 2
report = [1, 3, 2, 4, 5]
is_safe.(report)
is_safe.(List.delete_at(report, 2))
unsafe_reports = reports |> Enum.reject(is_safe)
dampen = fn report ->
Enum.reduce(0..Enum.count(report), is_safe.(report), fn
n, false -> is_safe.(List.delete_at(report, n))
_n, true -> true
end)
end
dampen.([4, 5, 4, 3, 2])
Enum.count(reports, dampen)