Advent of Code 2024 - Day 01
Mix.install([
{:req, "~> 0.5"}
])
Input
opts = [headers: [{"cookie", "session=#{System.fetch_env!("LB_AOC_SESSION")}"}]]
puzzle_input = Req.get!("https://adventofcode.com/2024/day/1/input", opts).body
{list_left, list_right} =
puzzle_input
|> String.split("\n", trim: true)
|> Enum.map(fn row ->
String.split(row)
|> Enum.map(&String.to_integer/1)
|> List.to_tuple()
end)
|> Enum.unzip()
Part 1
Enum.zip(
Enum.sort(list_left),
Enum.sort(list_right)
)
|> Enum.map(fn {a, b} -> abs(a - b) end)
|> Enum.sum()
Part 2
frequencies = Enum.frequencies(list_right)
Enum.reduce(list_left, 0, fn a, acc ->
acc + a * Map.get(frequencies, a, 0)
end)