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

Advent of Code 2024 Day 2 Part 2

2024_day2_part2.livemd

Advent of Code 2024 Day 2 Part 2

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 ->
  deleted =
    1..length(report)
    |> Enum.map(fn index ->
      Enum.take(report, index - 1) ++ Enum.drop(report, index)
    end)

  [report | deleted]
  |> Enum.filter(fn sub_report ->
    Enum.sort(sub_report) == sub_report or Enum.sort(sub_report, :desc) == sub_report
  end)
  |> Enum.find(fn sub_report ->
    next_report = tl(sub_report) ++ [nil]

    Enum.zip([sub_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)
  |> then(&amp;!is_nil(&amp;1))  
end)
|> length()