Advent of Code 2024
Mix.install([
{:req, "~> 0.3.2"}
])
Day 1
input =
"https://adventofcode.com/2024/day/1/input"
|> Req.get!(headers: [cookie: "session=#{System.get_env("AOC_COOKIE")}"])
|> Map.get(:body)
sample = """
3 4
4 3
2 5
1 3
3 9
3 3
"""
defmodule Day1 do
def parse(input) do
input
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
line
|> String.split(" ")
|> Enum.map(&String.to_integer/1)
end)
end
end
import Day1
Part 1
input
|> parse()
|> Enum.zip_with(&Enum.sort/1)
# |> Enum.zip()
# |> Enum.map(fn {a, b} -> abs(a-b) end)
# |> Enum.sum()
|> Enum.zip_reduce(0, fn [a, b], sum -> sum + abs(a-b) end)
Part 2
input
|> parse()
|> Enum.zip_with(&Enum.frequencies/1)
|> then(fn [a, b] ->
Enum.map(a, fn {k, v} -> k * v * Map.get(b, k, 0) end)
end)
|> Enum.sum()