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

Advent of Code - 2024 Day 2

2024/day-02.livemd

Advent of Code - 2024 Day 2

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

Preparation

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

Part 1

puzzle_input
|> String.split("\n")
|> Enum.map(&String.split/1)
|> Enum.map(&Enum.map(&1, fn s -> String.to_integer(s) end))
|> Enum.count(fn
  [a, a | _] ->
    false
  
  [a, b | _] = line ->
    sign = div(a - b, abs(a - b))
  
    line
    |> Enum.chunk_every(2, 1, :discard)
    |> Enum.all?(fn [a, b] ->
      sign * (a - b) in 1..3
    end)
end)

Part 2

puzzle_input
|> String.split("\n")
|> Enum.map(&String.split/1)
|> Enum.map(&Enum.map(&1, fn s -> String.to_integer(s) end))
|> Enum.count(fn line ->
  0..length(line)
  |> Stream.map(&List.delete_at(line, &1))
  |> Enum.any?(fn
    [a, a | _] ->
      false
  
    [a, b | _] = line ->
      sign = div(a - b, abs(a - b))
  
      line
      |> Enum.chunk_every(2, 1, :discard)
      |> Enum.all?(fn [a, b] ->
        sign * (a - b) in 1..3
      end)
  end)
end)