Powered by AppSignal & Oban Pro

Day 2

2024/day02.livemd

Day 2

Mix.install([{:utils, path: "#{__DIR__}/utils"}])

Solution

reports = Utils.read_2d_ints("day02")
safe? = fn report ->
  diffs = Enum.zip_with([report, tl(report)], fn [a, b] -> a - b end)
  gradual = diffs |> Enum.map(&abs/1) |> Enum.all?(&(1 <= &1 and &1 <= 3))
  monotonic = Enum.all?(diffs, &(&1 > 0)) or Enum.all?(diffs, &(&1 < 0))
  gradual and monotonic
end

Enum.count(reports, safe?)
tolerably_safe? = fn report ->
  1..Enum.count(report)
  |> Enum.map(&List.delete_at(report, &1 - 1))
  |> Enum.any?(safe?)
end

Enum.count(reports, tolerably_safe?)