AdventOfCode > 2026

input_year <- 2026
input_day <- 3
source(here::here("R/adventofcode.R"))
adventofcode_load()
input <- adventofcode_input(input_year, input_day, what = "character")

Part 1

example <- "#...#
.....
.....
.....
#...#"
trees <- example |>
  str_split_1("\n") |>
  map(\(x) str_split_1(x, "")) |>
  map(\(x) (x == "#")) |>
  do.call(rbind, args = _)
trees
##       [,1]  [,2]  [,3]  [,4]  [,5]
## [1,]  TRUE FALSE FALSE FALSE  TRUE
## [2,] FALSE FALSE FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE FALSE FALSE
## [5,]  TRUE FALSE FALSE FALSE  TRUE
has_square_corners <- \(trees, row, col, distance) {
  trees[row, col] &
    trees[row, col + distance] &
    trees[row + distance, col] &
    trees[row + distance, col + distance]
}

has_square_corners(trees, 1, 1, 4)
## [1] TRUE
example <- ".#..#
.....
.....
.#..#
....."
trees <- example |>
  str_split_1("\n") |>
  map(\(x) str_split_1(x, "")) |>
  map(\(x) (x == "#")) |>
  do.call(rbind, args = _)

has_square_corners <- \(trees, row, col, distance) {
  trees[row, col] &
    trees[row, col + distance] &
    trees[row + distance, col] &
    trees[row + distance, col + distance]
}

has_square_corners(trees, 1, 2, 3)
## [1] TRUE
example <- "#...#
.....
.....
.....
#...#"
trees <- example |>
  str_split_1("\n") |>
  map(\(x) str_split_1(x, "")) |>
  map(\(x) (x == "#")) |>
  do.call(rbind, args = _)

count_squares <- \(trees) {
  total_squares <- 0L
  distances <- seq_len(nrow(trees) - 1)
  for (distance in distances) {
    top_left_rows <- seq_len(nrow(trees) - distance)
    top_left_cols <- seq_len(ncol(trees) - distance)
    total_squares <- total_squares +
      sum(
        has_square_corners(trees, top_left_rows, top_left_cols, distance)
      )
  }

  total_squares
}

count_squares(trees)
## [1] 1
example <- ".#..#
.....
.....
.#..#
....."
trees <- example |>
  str_split_1("\n") |>
  map(\(x) str_split_1(x, "")) |>
  map(\(x) (x == "#")) |>
  do.call(rbind, args = _)

count_squares(trees)
## [1] 1
compute1 <- \(x) {
  x |>
    str_split_1("\n") |>
    map(\(x) str_split_1(x, "")) |>
    map(\(x) (x == "#")) |>
    do.call(rbind, args = _) |>
    count_squares()
}

#| label: part1_other_examples
example <- "#...#
.....
.....
.....
#...#"
expected <- 1L
result <- example |> compute1()
assertthat::are_equal(expected, result)
## [1] TRUE
example <- ".#..#
.....
.....
.#..#
....."
expected <- 1L
result <- example |> compute1()
assertthat::are_equal(expected, result)
## [1] TRUE
example <- ".....
.....
.##..
.##..
....."
expected <- 1L
result <- example |> compute1()
assertthat::are_equal(expected, result)
## [1] TRUE
example <- "#.###
.##.#
##.##
.#.#.
.##.#"
expected <- 1L
result <- example |> compute1()
assertthat::are_equal(expected, result)
## [1] TRUE
input <- adventofcode_input(input_year, input_day, what = "character")
result_part1 <- input |>
    compute1()

result_part1
## [1] 357

Part 2

example <- ".#...
....#
.....
#....
...#."
trees <- example |>
  str_split_1("\n") |>
  map(\(x) str_split_1(x, "")) |>
  map(\(x) (x == "#")) |>
  do.call(rbind, args = _)


# Start at (1, 2). The steps (0, 1) and (1, 3) both fit on the map.
hits <- which(trees)
x <- list(
  n_rows = nrow(trees),
  tree_rows = row(trees)[hits],
  tree_cols = col(trees)[hits],
  start_row = 1L,
  start_col = 2L
)


trees
##       [,1]  [,2]  [,3]  [,4]  [,5]
## [1,] FALSE  TRUE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE  TRUE
## [3,] FALSE FALSE FALSE FALSE FALSE
## [4,]  TRUE FALSE FALSE FALSE FALSE
## [5,] FALSE FALSE FALSE  TRUE FALSE
paste0("[", paste(x$tree_rows, x$tree_cols, sep = ","), "]")
## [1] "[4,1]" "[1,2]" "[5,4]" "[2,5]"
row_deltas <- x$tree_rows - x$start_row
row_deltas
## [1] 3 0 4 1
col_deltas <- x$tree_cols - x$start_col
col_deltas
## [1] -1  0  2  3
filter_valid_edges <- \(x, row_deltas, col_deltas) {
  valid_direction <- row_deltas >= 0L & col_deltas > 0L
  farthest_down_row <- x$tree_rows + col_deltas
  farthest_left_col <- x$start_col - row_deltas
  in_bounds <- farthest_down_row <= x$n_rows & farthest_left_col >= 1L
  fits <- valid_direction & in_bounds
  x$row_deltas <- row_deltas[fits]
  x$col_deltas <- col_deltas[fits]
  x
}

valid_edges <- filter_valid_edges(
  x,
  row_deltas = x$tree_rows - x$start_row,
  col_deltas = x$tree_cols - x$start_col
)

valid_edges$row_deltas
## [1] 1
valid_edges$col_deltas
## [1] 3
example <- ".#...
....#
.....
#....
...#."
trees <- example |>
  str_split_1("\n") |>
  map(\(x) str_split_1(x, "")) |>
  map(\(x) (x == "#")) |>
  do.call(rbind, args = _)
hits <- which(trees)
x <- list(
  n_rows = nrow(trees),
  tree_rows = row(trees)[hits],
  tree_cols = col(trees)[hits],
  start_row = 1L,
  start_col = 2L
)
trees
##       [,1]  [,2]  [,3]  [,4]  [,5]
## [1,] FALSE  TRUE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE  TRUE
## [3,] FALSE FALSE FALSE FALSE FALSE
## [4,]  TRUE FALSE FALSE FALSE FALSE
## [5,] FALSE FALSE FALSE  TRUE FALSE
valid_edges <- filter_valid_edges(
  x,
  row_deltas = x$tree_rows - x$start_row,
  col_deltas = x$tree_cols - x$start_col
)
start_of_edge <- c(
  row = x$start_row,
  col = x$start_col
)
edge_step <- c(
  row = valid_edges$row_deltas,
  col = valid_edges$col_deltas
)

rotate_deltas <- \(row_deltas, col_deltas) {
  list(row_deltas = col_deltas, col_deltas = -row_deltas)
}

turned_deltas <- rotate_deltas(
  valid_edges$row_deltas,
  valid_edges$col_deltas
)
turned_step <- c(row = turned_deltas$row_deltas, col = turned_deltas$col_deltas)

start_of_edge
## row col 
##   1   2
edge_step
## row col 
##   1   3
turned_step
## row col 
##   3  -1
example <- ".#...
....#
.....
#....
...#."
trees <- example |>
  str_split_1("\n") |>
  map(\(x) str_split_1(x, "")) |>
  map(\(x) (x == "#")) |>
  do.call(rbind, args = _)
hits <- which(trees)
x <- list(
  n_rows = nrow(trees),
  tree_rows = row(trees)[hits],
  tree_cols = col(trees)[hits],
  start_row = 1L,
  start_col = 2L
)

valid_edges <- filter_valid_edges(
  x,
  row_deltas = x$tree_rows - x$start_row,
  col_deltas = x$tree_cols - x$start_col
)

shift_edge_endpoints <- \(edges, turned_deltas) {
  end_row <- edges$start_row + edges$row_deltas
  end_col <- edges$start_col + edges$col_deltas

  from_start <- cbind(
    edges$start_row + turned_deltas$row_deltas,
    edges$start_col + turned_deltas$col_deltas
  )
  from_end <- cbind(
    end_row + turned_deltas$row_deltas,
    end_col + turned_deltas$col_deltas
  )

  list(from_start = from_start, from_end = from_end)
}

trees
##       [,1]  [,2]  [,3]  [,4]  [,5]
## [1,] FALSE  TRUE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE  TRUE
## [3,] FALSE FALSE FALSE FALSE FALSE
## [4,]  TRUE FALSE FALSE FALSE FALSE
## [5,] FALSE FALSE FALSE  TRUE FALSE
start_of_edge <- c(row = valid_edges$start_row, col = valid_edges$start_col)
start_of_edge
## row col 
##   1   2
shifted_endpoints <- shift_edge_endpoints(valid_edges, turned_deltas)
shifted_endpoints
## $from_start
##      [,1] [,2]
## [1,]    4    1
## 
## $from_end
##      [,1] [,2]
## [1,]    5    4
expected_endpoints <- list(
  from_start = matrix(c(4L, 1L), nrow = 1L),
  from_end = matrix(c(5L, 4L), nrow = 1L)
)

assertthat::assert_that(assertthat::are_equal(
  expected_endpoints,
  shifted_endpoints
))
## [1] TRUE
count_rotated_squares_from_start <- \(trees, x) {
  valid_edges <- filter_valid_edges(
    x,
    row_deltas = x$tree_rows - x$start_row,
    col_deltas = x$tree_cols - x$start_col
  )
  turned_deltas <- rotate_deltas(
    valid_edges$row_deltas,
    valid_edges$col_deltas
  )
  shifted <- shift_edge_endpoints(valid_edges, turned_deltas)

  sum(trees[shifted$from_start] & trees[shifted$from_end])
}


expected <- 1L
result <- count_rotated_squares_from_start(trees, x)
result
## [1] 1
assertthat::assert_that(assertthat::are_equal(expected, result))
## [1] TRUE
count_rotated_squares <- \(trees) {
    n_rows <- nrow(trees)
    hits <- which(trees)
    tree_rows <- row(trees)[hits]
    tree_cols <- col(trees)[hits]
    total_squares <- 0L

    for (i in seq_along(tree_rows)) {
        x <- list(
            n_rows = n_rows,
            tree_rows = tree_rows,
            tree_cols = tree_cols,
            start_row = tree_rows[i],
            start_col = tree_cols[i]
        )

        total_squares <- total_squares +
            count_rotated_squares_from_start(trees, x)
    }

    total_squares
}

compute2 <- \(x) {
    x |>
        str_trim() |>
        str_split_1("\n") |>
        map(\(row) str_split_1(row, "") == "#") |>
        do.call(rbind, args = _) |>
        count_rotated_squares()
}

example <- "#...#
.....
.....
.....
#...#"
assertthat::are_equal(1, compute2(example))
## [1] TRUE
example <- ".#...
....#
.....
#....
...#."
assertthat::are_equal(1, compute2(example))
## [1] TRUE
example <- "..#..
.....
#...#
.....
..#.."
assertthat::are_equal(1, compute2(example))
## [1] TRUE
example <- "....#..
.......
#......
.......
......#
.......
..#...."
assertthat::are_equal(1, compute2(example))
## [1] TRUE
example <- "#..#..#
.......
.......
#..#..#
.......
.......
#..#..#"
assertthat::are_equal(6, compute2(example))
## [1] TRUE
example <- "..#....
....#..
.#....#
...#...
#....#.
..#....
....#.."
assertthat::are_equal(6, compute2(example))
## [1] TRUE
example <- "#.###
.##.#
##.##
.#.#.
.##.#"
assertthat::are_equal(5, compute2(example))
## [1] TRUE
example <- "#######
#######
#######
#######
#######
#######
#######"
assertthat::are_equal(196, compute2(example))
## [1] TRUE
result_part2 <- input |>
    compute2()

result_part2
## [1] 1588