Solve day 2
This commit is contained in:
+24
-5
@@ -1,13 +1,32 @@
|
|||||||
defmodule Aoc2024.Day02 do
|
defmodule Aoc2024.Day02 do
|
||||||
def parse(input) do
|
def parse(input) do
|
||||||
input |> String.split("\n", trim: true)
|
Aoc2024.Utils.parse_rows_of_integers(input)
|
||||||
end
|
end
|
||||||
|
|
||||||
def part1(_lines) do
|
def part1(reports) do
|
||||||
:todo1
|
reports |> Enum.count(&isValid/1)
|
||||||
end
|
end
|
||||||
|
|
||||||
def part2(_lines) do
|
defp isValid(report) do
|
||||||
:todo2
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user