library(tidyverse)
my_file <- here::here("2021", "data-2021-02.txt")
x <- readLines(my_file)2021 Day 2
— Day 2: Dive! —
We have to figure out where the submarine is going.
Calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?
up and down mean different things, but we can handle that.
# create a helper object to provide instructions
effect <- tribble(
~direction, ~x, ~depth,
"forward", 1, 0,
"down", 0, 1,
"up", 0, -1
)
# create direction and value in their own columns
instruct <- tibble(inpt = x) %>%
separate(inpt, c("direction", "val"), convert = TRUE)
# join and calculate change at each step
delta_sub <- inner_join(instruct, effect, by = c("direction")) %>%
mutate(x_delta = val * x,
depth_delta = val * depth)
# part one answer is sum of final x and depth position
sum(delta_sub$x_delta) * sum(delta_sub$depth_delta)[1] 1451208
— Part Two —
Actually, the instructions are different.
Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?
# create a helper object to provide instructions
effect <- tribble(
~direction, ~x, ~dp,
"forward", 1, 0,
"down", 0, 1,
"up", 0, -1
)
# create direction and value in their own columns
instruct <- tibble(inpt = x) %>%
separate(inpt, c("direction", "val"), convert = TRUE)
# join and calculate change at each step
delta_sub <- inner_join(instruct, effect, by = c("direction")) %>%
mutate(x_delta = val * x,
depth_delta = val * dp,
aim = cumsum(depth_delta),
depth_aim = aim * x_delta)
# part one answer is sum of final x and depth position
sum(delta_sub$x_delta) * sum(delta_sub$depth_aim)[1] 1620141160