R/2-2.R

Defines functions dive2

Documented in dive2

#' @rdname day2
#' @param dat dat
#' @export
#'
dive2 <- function(dat) {
  # Initialise variables
  aim <- 0
  horizontal_position <- 0
  depth <- 0

  # Track aim
  for (i in seq_len(nrow(dat))) {
    if (dat$direction[i] == "down") {
      aim <- aim  + dat$value[i]
    } else if (dat$direction[i] == "up") {
      aim <- aim  - dat$value[i]
    } else {
      horizontal_position <- horizontal_position + dat$value[i]
      depth <- depth + (aim * dat$value[i])
    }
  }

  horizontal_position * depth
}
soniamitchell/adventofcode documentation built on Jan. 8, 2022, 9:33 a.m.