Powered by AppSignal & Oban Pro

Advent of Code 2024 Day 2 Part 1

2024_day2_part1.livemd

Advent of Code 2024 Day 2 Part 1

Mix.install([
  {:kino_aoc, "~> 0.1"}
])

Get Inputs

{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2024", "2", System.fetch_env!("LB_SESSION"))

My answer

reports =
  puzzle_input
  |> String.split("\n")
  |> Enum.map(fn row ->
    row
    |> String.split(" ")
    |> Enum.map(&String.to_integer(&1))
  end)
reports
|> Enum.filter(fn report ->
  Enum.sort(report) == report or Enum.sort(report, :desc) == report
end)
|> Enum.filter(fn report ->
  next_report = tl(report) ++ [nil]

  Enum.zip([report, next_report])
  |> Enum.all?(fn {current, next} ->
    if is_nil(next) do
      true
    else
      diff = abs(next - current)
      diff > 0 and diff < 4
    end
  end)
end)
|> length()