Each puzzle description typically comes with a few simple examples of inputs and outputs. We can use these to define expectations for unit tests with the testthat package. Once a function passes the unit tests, it should be ready to try with the main puzzle input.
Check my adventofcode2021 repository on GitHub for the latest.
Day 1 – Sonar Sweep
To count the number of times elements are increasing in a vector it’s as simple as
depths <- c(199, 200, 208, 210, 200, 207, 240, 269, 260, 263) sum(diff(depths) > 0) ##  7
for which I defined a function called
For part two, we first want to calculate the three-depth moving sum, then we count the increases as in part one.
There are plenty of solutions in external R packages for getting lagged (and leading) vectors, for instance
depths + dplyr::lead(depths) + dplyr::lead(depths, 2) ##  607 618 618 617 647 716 769 792 NA NA
Or you could even calculate the rolling sum using a pre-made solution in zoo (Z’s Ordered Observations, a time-series analysis package).
zoo::rollsum(depths, 3) ##  607 618 618 617 647 716 769 792
To avoid loading any external packages at this stage, I defined my own base R function called
rolling_sum(), which uses
head with negative lengths to omit the first and last elements of the vector:
head(depths, -2) + head(tail(depths, -1), -1) + tail(depths, -2) ##  607 618 618 617 647 716 769 792
As David Schoch points out, you can just use the
lag argument of
diff to make this entire puzzle into a one-liner:
sapply(c(1, 3), \(lag) sum(diff(depths, lag) > 0)) ##  7 5