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

Day 4

2023/day04.livemd

Day 4

Mix.install([
  {:kino, "~> 0.11.0"}
])

Section

input = Kino.Input.textarea("Input")
winners =
  input
  |> Kino.Input.read()
  |> String.split("\n")
  |> Enum.map(fn card ->
    card
    |> String.split([": ", "| "])
    |> then(fn [_game, winning_numbers, my_numbers] ->
      MapSet.intersection(
        winning_numbers
        |> String.split(" ", trim: true)
        |> Enum.map(&String.to_integer/1)
        |> MapSet.new(),
        my_numbers
        |> String.split(" ", trim: true)
        |> Enum.map(&String.to_integer/1)
        |> MapSet.new()
      )
    end)
    |> MapSet.size()
  end)
Enum.reduce(winners, 0, fn
  0, sum ->
    sum

  n, sum ->
    sum + Integer.pow(2, n - 1)
end)
winners
|> Enum.reverse()
|> Enum.reduce([], fn n, duplicates ->
  duplicates
  |> Enum.split(n)
  |> then(fn {head, tail} ->
    [Enum.sum([1 | head]) | head] ++ tail
  end)
end)
|> Enum.sum()