53 lines
842 B
Markdown
53 lines
842 B
Markdown
# Day01
|
|
|
|
```elixir
|
|
Mix.install([
|
|
{:kino, "~> 0.14.2"},
|
|
{:decimal, "~> 2.2"}
|
|
])
|
|
```
|
|
|
|
## Run Me
|
|
|
|
[](https://livebook.dev/run?url=https%3A%2F%2Fgit.kev.pub%2Fkev%2Faoc2024%2Fraw%2Fbranch%2Fmain%2Fday01.livemd)
|
|
|
|
## Input
|
|
|
|
```elixir
|
|
input = Kino.Input.textarea("Input")
|
|
```
|
|
|
|
## Parse Input
|
|
|
|
```elixir
|
|
input =
|
|
input
|
|
|> Kino.Input.read
|
|
|> String.split("\n")
|
|
|> Enum.map(fn l -> String.split(l, " ") end)
|
|
|> List.foldl([[],[]], fn [x, y], [xa, ya] ->
|
|
[[String.to_integer(x)| xa], [String.to_integer(y)| ya]]
|
|
end)
|
|
```
|
|
|
|
## Part One
|
|
|
|
```elixir
|
|
input
|
|
|> Enum.map(&Enum.sort/1)
|
|
|> Enum.zip
|
|
|> Enum.map(fn {x,y} -> abs(x - y) end)
|
|
|> Enum.sum
|
|
```
|
|
|
|
## Part Two
|
|
|
|
```elixir
|
|
[x, y] = input
|
|
|
|
f = Enum.frequencies(y)
|
|
|
|
Enum.map(x, fn xx -> xx * (f[xx] || 0) end)
|
|
|> Enum.sum
|
|
```
|