Use Livebook to open this notebook and explore new ideas.
It is easy to get started, on your machine or the cloud.
# Advent of Code 2024 - Day 01
```elixir
Mix.install([
{:req, "~> 0.5"}
])
```
## Input
```elixir
opts = [headers: [{"cookie", "session=#{System.fetch_env!("LB_AOC_SESSION")}"}]]
puzzle_input = Req.get!("https://adventofcode.com/2024/day/1/input", opts).body
```
```elixir
{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
```elixir
Enum.zip(
Enum.sort(list_left),
Enum.sort(list_right)
)
|> Enum.map(fn {a, b} -> abs(a - b) end)
|> Enum.sum()
```
## Part 2
```elixir
frequencies = Enum.frequencies(list_right)
```
```elixir
Enum.reduce(list_left, 0, fn a, acc ->
acc + a * Map.get(frequencies, a, 0)
end)
```
[](https://livebook.dev/run?url=https%3A%2F%2Fgithub.com%2Fpehbehbeh%2Fadventofcode%2Fblob%2Fmain%2F2024%2F01.livemd)
<!-- livebook:{"offset":978,"stamp":{"token":"XCP.IJ3cF5FAnxJRBUQnXfkWdT-LfAZB2UOaOhdH1Y75o4bgTZ-Pw1F1qtJaqNsr6SrVRcXAxVP_r5qwyy2X55N70UxEUXBiT6rbX-4sj3pzpzzPP0x36Rk","version":2}} -->
See source