Solve day 2

This commit is contained in:
Aurélien Geron
2024-12-07 12:39:01 +13:00
parent 6925d4b44e
commit 57506bfb8c
+24 -5
View File
@@ -1,13 +1,32 @@
defmodule Aoc2024.Day02 do
def parse(input) do
input |> String.split("\n", trim: true)
Aoc2024.Utils.parse_rows_of_integers(input)
end
def part1(_lines) do
:todo1
def part1(reports) do
reports |> Enum.count(&isValid/1)
end
def part2(_lines) do
:todo2
defp isValid(report) do
diffs =
report
|> Enum.zip(tl(report))
|> Enum.map(fn {a, b} -> b - a end)
diffs |> Enum.all?(fn diff -> diff in [1, 2, 3] end) ||
diffs |> Enum.all?(fn diff -> diff in [-1, -2, -3] end)
end
def part2(reports) do
reports |> Enum.count(&isValidWithProblemDampener/1)
end
defp isValidWithProblemDampener(report) do
isValid(report) ||
report
|> Enum.with_index()
|> Enum.any?(fn {_elem, index} ->
isValid(report |> List.delete_at(index))
end)
end
end