diff --git a/2022/elixir-livebook.livemd b/2022/elixir-livebook.livemd new file mode 100644 index 0000000..32e3c2d --- /dev/null +++ b/2022/elixir-livebook.livemd @@ -0,0 +1,87 @@ + + +# 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 +``` + + + +``` +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()}") +``` + + + +``` +Part 1: 71924 +Part 2: 210406 +``` + + + +``` +: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 + <>, {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}") +``` + + + +``` +Part 1: 12535 +Part 2: 15457 +``` + + + +``` +:ok +```