2020: Day 3

tidyverse
matrix
Author

Ella Kaye

Published

December 3, 2020

Setup

The original challenge

My data

Part 1

Starting at the top left corner of the map, how many trees (“#”) do we encounter, going at a trajectory of 3 right and 1 down?

First, read in the data and save it into a matrix. My method here feels really hack-y. I’m sure there must be a better approach.

library(tidyverse)

tree_map <- 
  read_tsv(here::here("2020", "day", "3", "input"), col_names = FALSE)

num_col <- tree_map %>%
  mutate(length = str_length(X1)) %>%
  slice(1) %>%
  pull(length)

tree_vec <- tree_map %>%
  mutate(X1 = strsplit(X1, split = character(0), fixed = TRUE)) %>%
  pull(X1) %>%
  unlist()

tree_mat <- matrix(tree_vec, ncol = num_col, byrow = TRUE)

Now work my way across and down the matrix, using the %% modulo operator to loop round where necessary. The -1 and +1 in the line ((y + right - 1) %% num_col) + 1 is a hack to get round the fact that, for num_col columns, the modulo runs from 0 to num_col - 1, but the column indexes for our matrix run from 1 to num_col.

right <- 3
down <- 1

num_rows <- nrow(tree_mat)
num_col <- ncol(tree_mat)

# start counting trees encountered
trees <- 0

# start square
x <- 1
y <- 1
  
while (x <= num_rows) {
  
  # cat("row: ", x, "col: ", y, "\n")
  
  if (tree_mat[x,y] == "#") trees <- trees + 1
  
  x <- x + down
  y <- ((y + right - 1) %% num_col) + 1
  
}

trees
[1] 299

Part 2

We now need to check several other trajectories, and multiply together the number of trees we find, so we wrap the Part 1 code into a function.

slope_check <- function(tree_mat, right, down) {
  
  num_rows <- nrow(tree_mat)
  num_col <- ncol(tree_mat)

  # start counting trees encountered
  trees <- 0

  # start square
  x <- 1
  y <- 1
  
  while (x <= num_rows) {
  
    if (tree_mat[x,y] == "#") trees <- trees + 1
  
    x <- x + down
    y <- ((y + right - 1) %% num_col) + 1
  
  }
  trees
}

prod(slope_check(tree_mat, 1, 1),
     slope_check(tree_mat, 3, 1),
     slope_check(tree_mat, 5, 1),
     slope_check(tree_mat, 7, 1),
     slope_check(tree_mat, 1, 2))
[1] 3621285278

Session info

Toggle
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.1 (2023-06-16)
 os       macOS Sonoma 14.0
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       Europe/London
 date     2023-11-06
 pandoc   3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
 quarto   1.4.466 @ /usr/local/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 dplyr       * 1.1.2   2023-04-20 [1] CRAN (R 4.3.0)
 forcats     * 1.0.0   2023-01-29 [1] CRAN (R 4.3.0)
 ggplot2     * 3.4.2   2023-04-03 [1] CRAN (R 4.3.0)
 lubridate   * 1.9.2   2023-02-10 [1] CRAN (R 4.3.0)
 purrr       * 1.0.1   2023-01-10 [1] CRAN (R 4.3.0)
 readr       * 2.1.4   2023-02-10 [1] CRAN (R 4.3.0)
 sessioninfo * 1.2.2   2021-12-06 [1] CRAN (R 4.3.0)
 stringr     * 1.5.0   2022-12-02 [1] CRAN (R 4.3.0)
 tibble      * 3.2.1   2023-03-20 [1] CRAN (R 4.3.0)
 tidyr       * 1.3.0   2023-01-24 [1] CRAN (R 4.3.0)
 tidyverse   * 2.0.0   2023-02-22 [1] CRAN (R 4.3.0)

 [1] /Users/ellakaye/Library/R/arm64/4.3/library
 [2] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library

──────────────────────────────────────────────────────────────────────────────