Day 2

Advent of Code: Worked Solutions

About
Date

December 2, 2021

Setup

Import libraries:

library(tidyverse)

Read input from file:

input <- read_delim(
  "../input/day02.txt", 
  delim = " ", 
  col_names = c("dir", "value"),
  show_col_types = FALSE
)

Part 1

Convert input values to final horizontal positions and depth values:

pos <- input |> 
  transmute(
    dim   = case_match(dir, "forward" ~ "horiz", c("up", "down") ~ "depth"),
    value = case_match(dir, "up" ~ -value, .default = value)
  ) |> 
  summarize(value = sum(value), .by = dim)

Compute product of final horizontal position and depth:

prod(pos$value)

Part 2

Compute final horizontal position and depth value using the modified keyword meanings:

pos <- input |> 
  transmute(
    aim   = cumsum(case_match(dir, "down" ~ value, "up" ~ -value, .default = 0)),
    horiz = cumsum(case_match(dir, "forward" ~ value,             .default = 0)),
    depth = cumsum(case_match(dir, "forward" ~ value * aim,       .default = 0))
  ) |> 
  select(horiz, depth) |> 
  tail(n = 1)

Compute product of final horizontal position and depth:

pos$horiz * pos$depth