# Libraries
library(tidyverse)
# Read input from file
<- read_lines("../input/day01.txt") |>
input as.integer()
Day 1
Advent of Code: Worked Solutions
Setup
Part 1
# Format input as a data frame and number the elves
<- tibble(
df cal = input,
elf_id = cumsum(is.na(cal)) + 1
|>
) filter(!is.na(cal))
# Compute calorie sum for each elf, get the top n elves, and combine totals
<- function(df, num_top_elves) {
count_max |>
df group_by(elf_id) |>
summarize(total_cal = sum(cal)) |>
slice_max(total_cal, n = num_top_elves) |>
pull(total_cal) |>
sum()
}
Run puzzle input:
count_max(df, 1)
[1] 68787
Part 2
count_max(df, 3)
[1] 198041