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

Day 2

2024/day02.livemd

Day 2

Mix.install([:kino])

Section

input = Kino.Input.textarea("Input", monospace: true)
reports =
  input
  |> Kino.Input.read()
  |> String.split("\n", trim: true)
  |> Enum.map(fn report ->
    report
    |> String.split(" ", trim: true)
    |> Enum.map(&String.to_integer/1)
  end)
defmodule RedNosedReport do
  def safe?(report) do
    monotonic?(report) or
      monotonic?(Enum.reverse(report))
  end

  defp monotonic?(report, max \\ 4) do
    report
    |> Enum.chunk_every(2, 1, :discard)
    |> Enum.all?(fn [a, b] -> a < b and b - a < max end)
  end
end

Enum.count(reports, &amp;RedNosedReport.safe?/1)
defmodule ProblemDampener do
  def safe?(left \\ [], right)

  def safe?(left, [r | right]), do:
    RedNosedReport.safe?(left ++ right) or
      safe?(left ++ [r], right)
  end

  def safe?(report, []) do
    RedNosedReport.safe?(report)
  end
end

Enum.count(reports, &amp;ProblemDampener.safe?/1)