day 2 part 2 done naively
This commit is contained in:
parent
d6b6715e3d
commit
e49e64ceef
88
lib/day02.ex
88
lib/day02.ex
@ -1027,78 +1027,66 @@ defmodule AdventOfCode.Day02 do
|
||||
end
|
||||
|
||||
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.map(fn {level, index} ->
|
||||
is_last_level = index == length(report) - 1
|
||||
|
||||
case is_last_level do
|
||||
true -> true
|
||||
false -> level < Enum.at(report, index + 1)
|
||||
|> Enum.reduce(true, fn {level, index}, acc ->
|
||||
case index == length(report) - 1 do
|
||||
true -> acc
|
||||
false -> acc and level < Enum.at(report, index + 1)
|
||||
end
|
||||
end)
|
||||
|> Enum.filter(fn item -> item == false end)
|
||||
|> length()
|
||||
end
|
||||
|
||||
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.map(fn {level, index} ->
|
||||
is_last_level = index == length(report) - 1
|
||||
|
||||
case is_last_level do
|
||||
true -> true
|
||||
false -> level > Enum.at(report, index + 1)
|
||||
|> Enum.reduce(true, fn {level, index}, acc ->
|
||||
case index == length(report) - 1 do
|
||||
true -> acc
|
||||
false -> acc and level > Enum.at(report, index + 1)
|
||||
end
|
||||
end)
|
||||
|> IO.inspect()
|
||||
|> Enum.filter(fn item -> item == false end)
|
||||
|> length()
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
defp count_outside_limits(report) do
|
||||
Enum.with_index(report)
|
||||
|> Enum.map(fn {level, index} ->
|
||||
is_last_level = index == length(report) - 1
|
||||
def part2() do
|
||||
reports = parse_reports(@input)
|
||||
first_round = reports |> Enum.map(fn report -> is_safe?(report) end)
|
||||
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
|
||||
|
||||
false ->
|
||||
difference = abs(level - Enum.at(report, index + 1))
|
||||
difference >= 1 and difference <= 3
|
||||
List.delete_at(report, index) |> is_safe?()
|
||||
end
|
||||
end)
|
||||
|> Enum.filter(fn item -> item == false end)
|
||||
end
|
||||
end)
|
||||
|> Enum.filter(fn result -> result == true end)
|
||||
|> length()
|
||||
end
|
||||
|
||||
defp count_unsafe(report) do
|
||||
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()
|
||||
safe_results + potentially_safe_results
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user