# Libraries
library(tidyverse)
# Read input from file
input <- read_lines("../input/day01.txt") |>
as.integer()Day 1
Advent of Code: Worked Solutions
Setup
Part 1
# Format input as a data frame and number the elves
df <- tibble(
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
count_max <- function(df, num_top_elves) {
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)Part 2
count_max(df, 3)