day 2 part 2 done naively

This commit is contained in:
m5r 2024-12-04 00:16:54 +01:00
parent d6b6715e3d
commit e49e64ceef
Signed by: mokhtar
GPG Key ID: 1509B54946D08A95

View File

@ -1027,78 +1027,66 @@ defmodule AdventOfCode.Day02 do
end end
defp is_ascending_order?(report) do defp is_ascending_order?(report) do
count_out_of_order_ascending(report) == 0
end
defp count_out_of_order_ascending(report) do
Enum.with_index(report) Enum.with_index(report)
|> Enum.map(fn {level, index} -> |> Enum.reduce(true, fn {level, index}, acc ->
is_last_level = index == length(report) - 1 case index == length(report) - 1 do
true -> acc
case is_last_level do false -> acc and level < Enum.at(report, index + 1)
true -> true
false -> level < Enum.at(report, index + 1)
end end
end) end)
|> Enum.filter(fn item -> item == false end)
|> length()
end end
defp is_descending_order?(report) do defp is_descending_order?(report) do
count_out_of_order_descending(report) == 0
end
defp count_out_of_order_descending(report) do
Enum.with_index(report) Enum.with_index(report)
|> Enum.map(fn {level, index} -> |> Enum.reduce(true, fn {level, index}, acc ->
is_last_level = index == length(report) - 1 case index == length(report) - 1 do
true -> acc
case is_last_level do false -> acc and level > Enum.at(report, index + 1)
true -> true
false -> level > Enum.at(report, index + 1)
end end
end) end)
|> IO.inspect()
|> Enum.filter(fn item -> item == false end)
|> length()
end end
defp difference_is_within_limits?(report) do defp difference_is_within_limits?(report) do
count_outside_limits(report) == 0 Enum.with_index(report)
|> Enum.reduce(true, fn {level, index}, acc ->
case index == length(report) - 1 do
true ->
acc
false ->
difference = abs(level - Enum.at(report, index + 1))
acc and difference >= 1 and difference <= 3
end
end)
end end
defp count_outside_limits(report) do def part2() do
Enum.with_index(report) reports = parse_reports(@input)
|> Enum.map(fn {level, index} -> first_round = reports |> Enum.map(fn report -> is_safe?(report) end)
is_last_level = index == length(report) - 1 safe_results = Enum.filter(first_round, fn result -> result == true end) |> length()
case is_last_level do potentially_safe_results =
Enum.map(reports, fn report ->
case is_safe?(report) do
true ->
nil
false ->
Enum.with_index(report)
|> Enum.reduce(false, fn {_level, index}, acc ->
case acc do
true -> true ->
true true
false -> false ->
difference = abs(level - Enum.at(report, index + 1)) List.delete_at(report, index) |> is_safe?()
difference >= 1 and difference <= 3
end end
end) end)
|> Enum.filter(fn item -> item == false end) end
end)
|> Enum.filter(fn result -> result == true end)
|> length() |> length()
end
defp count_unsafe(report) do safe_results + potentially_safe_results
outside_limits = count_outside_limits(report)
out_of_order_ascending = count_out_of_order_ascending(report)
out_of_order_descending = count_out_of_order_descending(report)
IO.inspect({outside_limits, out_of_order_ascending, out_of_order_descending})
outside_limits + out_of_order_ascending + out_of_order_descending
end
def part2() do
parse_reports(@input_test)
|> Enum.map(fn report -> count_unsafe(report) end)
|> Enum.filter(fn result -> result <= 1 end)
|> length()
end end
end end