2021 Day 1

Author

Nathan Moore

— Day 1: Sonar Sweep —

You’re on a submarine trying to recover the keys for Santa’s sleigh.

Use the sonar readouts to check the depth of the ocean.

How many measurements are larger than the previous measurement?

library(tidyverse)

my_file <- here::here("2021", "data-2021-01.txt")
x <- readLines(my_file)

diff function gives us what we need

x <- as.numeric(x)
x_lag <- diff(x) > 0
p1 <- sum(x_lag)

p1
[1] 1752

— Part Two —

Actually,

Consider sums of a three-measurement sliding window. How many sums are larger than the previous sum?

diff function has a lag argument!

x <- as.numeric(x)
x_lag <- diff(x, lag = 3) > 0
p2 <- sum(x_lag)

p2
[1] 1781