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.

Set-Up

When the blank lines are read in, each NA represents a new elf’s calorie entry

Code
calories <- as.integer(readLines("input.txt"))
head(calories, 40)
 [1] 18313  2404 10479    NA  7011 10279  1496 10342  8918  3162  4525  4368
[13]    NA 17242    NA 10920 14072  9754  4435  9396    NA  5915  2602  4032
[25]  3303  2685  1856  1334  4865  6385  1733  5328    NA  8899  5482  3195
[37]  7837  8986 13794    NA

Part 1

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?

Code
elves <- cumsum(is.na(calories))
elf_calories <- vapply(split(calories, elves), sum, integer(1L), na.rm = TRUE)
max(elf_calories)
[1] 71924

Part 2

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.

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

Code
sum(sort(elf_calories, decreasing = TRUE)[1:3])
[1] 210406