Day 1

Advent of Code: Worked Solutions

About
Date

December 1, 2021

Setup

Import libraries:

library(tidyverse)

Read input from file:

input <- read_lines("../input/day01.txt") |> as.integer()

Part 1

Define a function to count the number of increasing values in a sequence:

count_incr <- \(x) sum(x - lag(x) > 0, na.rm = TRUE)

Run on input:

count_incr(input)

Part 2

Count increases for 3-measurement windows:

count_incr(input + lead(input) + lead(input, 2))