Day 2
Mix.install([
{:req, "~> 0.4.5"},
{:kino, "~> 0.11.3"}
])
Input
session = System.fetch_env!("LB_AOC_SESSION")
input =
Req.get!(
"https://adventofcode.com/2023/day/2/input",
headers: [{"Cookie", ~s"session=#{session}"}]
).body
Kino.Text.new(input, terminal: true)
Part 1
defmodule Part1 do
def answer(text, %{} = bag) do
text
|> String.trim_trailing()
|> String.splitter("\n")
|> Enum.map(fn line ->
Regex.scan(~r/(\d+) (\w+)/, line)
|> Enum.map(fn [_, n, color] ->
{String.to_integer(n), color}
end)
|> Enum.reduce(%{}, fn {n, color}, acc ->
Map.put(acc, color, max(n, Map.get(acc, color, 0)))
end)
end)
|> Enum.with_index(1)
|> Enum.filter(fn {set, _} ->
Enum.all?(set, fn {key, value} -> value <= bag[key] end)
end)
|> Enum.map(&elem(&1, 1))
|> Enum.sum()
end
end
bag = %{"red" => 12, "green" => 13, "blue" => 14}
Part1.answer(input, bag)
Part 2
defmodule Part2 do
def answer(text) do
text
|> String.trim_trailing()
|> String.splitter("\n")
|> Enum.map(fn line ->
Regex.scan(~r/(\d+) (\w+)/, line)
|> Enum.map(fn [_, n, color] ->
{String.to_integer(n), color}
end)
|> Enum.reduce(%{}, fn {n, color}, acc ->
Map.put(acc, color, max(n, Map.get(acc, color, 0)))
end)
|> Map.values()
|> Enum.product()
end)
|> Enum.sum()
end
end
Part2.answer(input)