advent-of-code/2022/elixir-livebook.livemd
2022-12-02 15:52:38 -06:00

1.2 KiB

Advent of Code 2022

defmodule AoC do
  def input_file_contents(n), do: File.read!(Path.expand("~/.cache/aoc2022/#{n}.input"))
end

import Enum
Enum

Day 1

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

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}")
Part 1: 12535
Part 2: 15457
:ok