Add livebook
This commit is contained in:
parent
2bb350f1ab
commit
37f7aa6912
1 changed files with 87 additions and 0 deletions
87
2022/elixir-livebook.livemd
Normal file
87
2022/elixir-livebook.livemd
Normal file
|
@ -0,0 +1,87 @@
|
|||
<!-- livebook:{"persist_outputs":true} -->
|
||||
|
||||
# Advent of Code 2022
|
||||
|
||||
```elixir
|
||||
defmodule AoC do
|
||||
def input_file_contents(n), do: File.read!(Path.expand("~/.cache/aoc2022/#{n}.input"))
|
||||
end
|
||||
|
||||
import Enum
|
||||
```
|
||||
|
||||
<!-- livebook:{"output":true} -->
|
||||
|
||||
```
|
||||
Enum
|
||||
```
|
||||
|
||||
## Day 1
|
||||
|
||||
```elixir
|
||||
calories =
|
||||
AoC.input_file_contents(1)
|
||||
|> String.split("\n")
|
||||
|> reduce([0], fn
|
||||
"", l -> [0 | l]
|
||||
s, [n | l] -> [n + String.to_integer(s) | l]
|
||||
end)
|
||||
|> sort()
|
||||
|> reverse()
|
||||
|
||||
IO.puts("Part 1: #{hd(calories)}")
|
||||
IO.puts("Part 2: #{Enum.take(calories, 3) |> Enum.sum()}")
|
||||
```
|
||||
|
||||
<!-- livebook:{"output":true} -->
|
||||
|
||||
```
|
||||
Part 1: 71924
|
||||
Part 2: 210406
|
||||
```
|
||||
|
||||
<!-- livebook:{"output":true} -->
|
||||
|
||||
```
|
||||
:ok
|
||||
```
|
||||
|
||||
## Day 2
|
||||
|
||||
```elixir
|
||||
m = %{
|
||||
0 => %{0 => 4, 1 => 8, 2 => 3},
|
||||
1 => %{0 => 1, 1 => 5, 2 => 9},
|
||||
2 => %{0 => 7, 1 => 2, 2 => 6}
|
||||
}
|
||||
|
||||
points = fn
|
||||
<<a, _, b, 0x0A, rest::binary>>, {p1, p2}, r ->
|
||||
{y, x} = {a - 65, b - 88}
|
||||
r.(rest, {p1 + m[y][x], p2 + m[y][rem(3 + x + y - 1, 3)]}, r)
|
||||
|
||||
_, {p1, p2}, _ ->
|
||||
{p1, p2}
|
||||
end
|
||||
|
||||
{p1, p2} =
|
||||
2
|
||||
|> AoC.input_file_contents()
|
||||
|> points.({0, 0}, points)
|
||||
|
||||
IO.puts("Part 1: #{p1}")
|
||||
IO.puts("Part 2: #{p2}")
|
||||
```
|
||||
|
||||
<!-- livebook:{"output":true} -->
|
||||
|
||||
```
|
||||
Part 1: 12535
|
||||
Part 2: 15457
|
||||
```
|
||||
|
||||
<!-- livebook:{"output":true} -->
|
||||
|
||||
```
|
||||
:ok
|
||||
```
|
Loading…
Reference in a new issue