Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Advent of Code 2022 - Day 1

day01.livemd

Advent of Code 2022 - Day 1

Day 1: Calorie Counting

Description

Utils

defmodule Load do
  def file(path) do
    File.read!(__DIR__ <> path)
    |> String.split("\n")
  end

  def string(value) do
    value |> String.split("\n")
  end
end

Input

input = Load.file("/data/day01.txt")

Part 1

Find the Elf carrying the most Calories. How many total Calories is that Elf carrying?

defmodule Part1 do
  def calculate_totals(input) do
    input
    |> Enum.chunk_by(&amp;(&amp;1 == ""))
    |> Enum.reject(&amp;(&amp;1 === [""]))
    |> Enum.map(fn list ->
      list |> Enum.map(&amp;String.to_integer(&amp;1))
    end)
    |> Enum.map(&amp;Enum.sum/1)
  end
end

input
|> Part1.calculate_totals()
|> Enum.max()

Result

68292

Part 2

Find the top three Elves carrying the most Calories. How many Calories are those Elves carrying in total?

input
|> Part1.calculate_totals()
|> Enum.sort(&amp;(&amp;1 > &amp;2))
|> Enum.slice(0..2)
|> Enum.sum()

Result

203203