Advent of Code 2022 - Day 1
Day 1: Calorie Counting
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(&(&1 == ""))
|> Enum.reject(&(&1 === [""]))
|> Enum.map(fn list ->
list |> Enum.map(&String.to_integer(&1))
end)
|> Enum.map(&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(&(&1 > &2))
|> Enum.slice(0..2)
|> Enum.sum()
Result
203203