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

Day 5: Print Queue

2024/day05.livemd

Day 5: Print Queue

Mix.install([:kino])

Section

input = Kino.Input.textarea("Input", monospace: true)
{instructions, updates} =
  input
  |> Kino.Input.read()
  |> String.split("\n\n")
  |> then(fn [instructions, updates] ->
    {
      instructions,
      updates
      |> String.split()
      |> Enum.map(fn update ->
        update
        |> String.split(",")
        |> Enum.map(&String.to_integer/1)
      end)
    }
  end)
Enum.reduce(updates, {0, 0}, fn update, {sorted, unsorted} ->
  case Enum.sort(update, &(instructions =~ "#{&1}|#{&2}")) do
    ^update ->
      {sorted + Enum.at(update, div(length(update), 2)), unsorted}

    update ->
      {sorted, unsorted + Enum.at(update, div(length(update), 2))}
  end
end)