Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

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(&amp;abs/1) |> Enum.all?(&amp;(1 <= &amp;1 and &amp;1 <= 3))
  monotonic = Enum.all?(diffs, &amp;(&amp;1 > 0)) or Enum.all?(diffs, &amp;(&amp;1 < 0))
  gradual and monotonic
end

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

Enum.count(reports, tolerably_safe?)