Day 4
Mix.install([
{:kino_aoc, "~> 0.1.5"}
])
Input
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2023", "4", System.fetch_env!("LB_AOC_SESSION"))
scratchcards =
puzzle_input
|> String.split("\n")
|> Enum.map(fn s ->
Regex.scan(~r/\d+(?![:\d])/, s)
|> Enum.flat_map(& &1)
|> Enum.map(&String.to_integer/1)
|> Enum.split(10)
|> Tuple.to_list()
|> Enum.map(&MapSet.new/1)
end)
Part 1
win_counts =
scratchcards
|> Enum.map(&apply(MapSet, :intersection, &1))
|> Enum.map(&Enum.count/1)
win_counts
|> Enum.filter(&(&1 > 0))
|> Enum.map(&(:math.pow(2, &1 - 1) |> trunc))
|> Enum.sum()
Part 2
win_counts
|> Enum.reverse()
|> Enum.reduce([], fn wins, acc ->
[1 + Enum.sum(Enum.take(acc, wins)) | acc]
end)
|> Enum.sum()