base R
lists
Published

December 1, 2022

Setup

The original challenge

Part 1

I’m including this post in the website example to demonstrate what a typical post will look like, using post-template, in the _templates directory as a starting point. This post is created by a call to aoc_new_day(1, 2022). It can be deleted with a call to aoc_delete_day(1, 2022), or all posts and the listing for the year can be deleted with aoc_delete_year(2022).

Note

This is Ella Kaye’s solution1, with her puzzle input. If attempting this challenge yourself, your solution will be different.

Toggle the code
library(aochelpers)
input <- aoc_input_vector(1, 2022, mode = "numeric")

I’m using the aoc_input_vector() function from the aochelpers package to read in the data, but otherwise using base R functions (including the native pipe, |>) for this puzzle.

In this challenge, we’re given groups of numbers and we need to find the sum of each group. Our solution is the largest of these. The groups are separated by a blank line. When reading in the input as a numeric vector, these are coerced to NA. We can identify the new groups by the NA values, produce an index for them with cumsum(is.na(input)), which increments when a new NA is reached, then use this with split() to split the input into a list of vectors, one for each group. We need the argument na.rm = TRUE in sapply() because each vector, other than the first, starts with NA, as that’s where it was split.

Toggle the code
totals <- split(input, cumsum(is.na(input))) |> 
  sapply(sum, na.rm = TRUE) 

max(totals)
[1] 66719

Part 2

This is similar, except we want to find the sum of the sums of the top three groups.

Toggle the code
totals |> 
  sort() |> 
  tail(3) |> 
  sum()
[1] 198551

Footnotes

  1. Ella is the author of this website template and of the aochelpers package, and the author of this demo post.↩︎