diff --git a/lib/aoc2024/day_02.ex b/lib/aoc2024/day_02.ex index ab96507..3216822 100644 --- a/lib/aoc2024/day_02.ex +++ b/lib/aoc2024/day_02.ex @@ -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