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

Day 1: Calorie Counting

1.livemd

Day 1: Calorie Counting

Mix.install([
  {:benchee, "~> 1.1"},
  {:benchee_html, "~> 1.0"}
])
:ok

Story

The jungle must be too overgrown and difficult to navigate in vehicles or access from the air; the Elves’ expedition traditionally goes on foot. As your boats approach land, the Elves begin taking inventory of their supplies. One important consideration is food - in particular, the number of Calories each Elf is carrying (your puzzle input).

The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they’ve brought with them, one item per line. Each Elf separates their own inventory from the previous Elf’s inventory (if any) by a blank line.

For example, suppose the Elves finish writing their items’ Calories and end up with the following list:

1000
2000
3000

4000

5000
6000

7000
8000
9000

10000

This list represents the Calories of the food carried by five Elves:

  • The first Elf is carrying food with 1000, 2000, and 3000 Calories, a total of 6000 Calories.
  • The second Elf is carrying one food item with 4000 Calories.
  • The third Elf is carrying food with 5000 and 6000 Calories, a total of 11000 Calories.
  • The fourth Elf is carrying food with 7000, 8000, and 9000 Calories, a total of 24000 Calories.
  • The fifth Elf is carrying one food item with 10000 Calories.

In case the Elves get hungry and need extra snacks, they need to know which Elf to ask: they’d like to know how many Calories are being carried by the Elf carrying the most Calories. In the example above, this is 24000 (carried by the fourth Elf).

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

defmodule Advent1 do
  def solve() do
    File.stream!("#{File.cwd!()}/input/1.txt")
    |> Stream.map(&String.trim/1)
    |> Enum.to_list()
    |> Advent1.count()
  end

  def count(input_list) do
    count(input_list, [], 0)
  end

  defp count([head | tail], elf, max) when head == "" do
    elf_sum =
      elf
      |> Enum.map(&String.to_integer/1)
      |> Enum.sum()

    max = Enum.max([elf_sum, max])

    count(tail, [], max)
  end

  defp count([head | tail], elf, max) do
    elf = [head | elf]
    count(tail, elf, max)
  end

  defp count([], _, max) do
    max
  end
end

Advent1.solve()
71502

Part Two

By the time you calculate the answer to the Elves’ question, they’ve already realized that the Elf carrying the most Calories of food might eventually run out of snacks.

To avoid this unacceptable situation, the Elves would instead like to know the total Calories carried by the top three Elves carrying the most Calories. That way, even if one of those Elves runs out of snacks, they still have two backups.

In the example above, the top three Elves are the fourth Elf (with 24000 Calories), then the third Elf (with 11000 Calories), then the fifth Elf (with 10000 Calories). The sum of the Calories carried by these three elves is 45000.

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

defmodule Advent2 do
  def solve() do
    File.stream!("#{File.cwd!()}/input/1.txt")
    |> Stream.map(&String.trim/1)
    |> Enum.to_list()
    |> Advent2.count()
    |> Enum.sum()
  end

  def count(input_list) do
    count(input_list, [], [])
  end

  # When the current line is empty,
  # sum up the calories for this elf
  # and compare this elves calorie sum to the top 3 of the previous elves.
  # Continue with the remaining elves
  defp count([food | tail], elf, sums) when food == "" do
    elfsum =
      elf
      |> Enum.map(&String.to_integer/1)
      |> Enum.sum()

    top3_sums =
      [elfsum | sums]
      |> Enum.sort(:desc)
      |> Enum.take(3)

    count(tail, [], top3_sums)
  end

  # add the calories from this elf to its itemlist
  defp count([food | tail], elf, sums) do
    elf = [food | elf]
    count(tail, elf, sums)
  end

  # Last elf, return the calorie sums
  defp count([], _, sums) do
    sums
  end
end

Advent2.solve()
208191

Benchmarks

Benchee.run(%{
  "Part 1" => &Advent1.solve/0,
  "Part 2" => &Advent2.solve/0
})

:ok
Operating System: Linux
CPU Information: Intel(R) Core(TM) i7-5500U CPU @ 2.40GHz
Number of Available Cores: 4
Available memory: 7.69 GB
Elixir 1.14.0
Erlang 25.1.2

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
reduction time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 14 s

Benchmarking Part 1 ...
Benchmarking Part 2 ...

Name             ips        average  deviation         median         99th %
Part 1        502.39        1.99 ms    ±37.20%        1.70 ms        5.00 ms
Part 2        493.07        2.03 ms    ±32.32%        1.78 ms        4.51 ms

Comparison: 
Part 1        502.39
Part 2        493.07 - 1.02x slower +0.0376 ms
:ok