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

Day 2

2023/day02.livemd

Day 2

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

Section

input = Kino.Input.textarea("Input")
parsed =
  input
  |> Kino.Input.read()
  |> String.split("\n")
  |> Enum.map(&String.split(&1, [":", ";", ",", " "], trim: true))
  |> Enum.map(fn ["Game", index | sets] ->
    sets
    |> Enum.chunk_every(2)
    |> Enum.group_by(fn [_, c] -> c end, fn [n, _] -> String.to_integer(n) end)
    |> Map.new(fn {key, count} -> {key, Enum.max(count)} end)
    |> Map.put("index", String.to_integer(index))
  end)

P1

parsed
|> Enum.filter(
  &amp;(Map.get(&amp;1, "red", 0) <= 12 and Map.get(&amp;1, "green", 0) <= 13 and Map.get(&amp;1, "blue", 0) <= 14)
)
|> Enum.reduce(0, &amp;(&amp;1["index"] + &amp;2))

P2

parsed
|> Enum.map(fn map ->
  map
  |> Map.delete("index")
  |> Map.values()
  |> Enum.product()
end)
|> Enum.sum()