Day 2: Dive!

Click for Problem Statement

Back to 2021


library(tidyverse)
library(here)
path_data <- here("2021/inputs/02-input.txt")
input <- tibble(x = read_lines(path_data))

Part 1

This went quite well, but set me up for troubles in Part 2

# this works great, but "convert" will give troubles later
part_1 <- input %>% 
  separate(sep = " ", col = x, into = c("inst", "num"), convert = TRUE)

result <- part_1 %>% 
  group_by(inst) %>% 
  summarise(total = sum(num))

# input the answer by hand
# meaning (2053 - 1137) * 1845
result
## # A tibble: 3 × 2
##   inst    total
##   <chr>   <int>
## 1 down     2053
## 2 forward  1845
## 3 up       1137
answering <- result %>% pull(total)

(answering[1] - answering[3]) * answering[2]
## [1] 1690020

Part 2

case_when got mad since separate converted to integers, which confuzzled me for a long while. Not converting was the way to go, control as numeric.

Use a bunch of cumulative sums with some selective interim columns worked.

part_2 <- input %>% 
  separate(sep = " ", col = x, into = c("inst", "num")) %>% 
  mutate(num = as.numeric(num))

# create interim columns to define conitional changes
# sum them up
travel <- part_2 %>% 
  mutate(aiming = case_when(
    inst == "up" ~ num * -1,
    inst == "down" ~ num,
    inst == "forward" ~ 0
  )) %>% 
  mutate(aim_total = cumsum(aiming)) %>% 
  mutate(horiz = ifelse(inst == "forward", num, 0),
         horiz_total = cumsum(horiz)) %>% 
  mutate(depth_increase = horiz * aim_total,
         depth_total = cumsum(depth_increase))

travel
## # A tibble: 1,000 × 8
##    inst      num aiming aim_total horiz horiz_total depth_increase depth_total
##    <chr>   <dbl>  <dbl>     <dbl> <dbl>       <dbl>          <dbl>       <dbl>
##  1 forward     4      0         0     4           4              0           0
##  2 down        8      8         8     0           4              0           0
##  3 down        8      8        16     0           4              0           0
##  4 up          2     -2        14     0           4              0           0
##  5 up          7     -7         7     0           4              0           0
##  6 forward     5      0         7     5           9             35          35
##  7 forward     5      0         7     5          14             35          70
##  8 up          7     -7         0     0          14              0          70
##  9 down        6      6         6     0          14              0          70
## 10 down        3      3         9     0          14              0          70
## # … with 990 more rows
# did this by hand, take the last row
# depth_total * horiz_total
answering2 <- travel %>% 
  tail(1)
answering2
## # A tibble: 1 × 8
##   inst      num aiming aim_total horiz horiz_total depth_increase depth_total
##   <chr>   <dbl>  <dbl>     <dbl> <dbl>       <dbl>          <dbl>       <dbl>
## 1 forward     4      0       916     4        1845           3664      763408
answering2$depth_total * answering2$horiz_total
## [1] 1408487760

Okay byeeeeeeeeee