AdventOfCode > 2026
input_year <- 2026
input_day <- 2
source(here::here("R/adventofcode.R"))
adventofcode_load()
input <- adventofcode_input(input_year, input_day, what = "character")
expect_example1 <- 16L
example1 <- tibble(x = chars("obbvb"))
waypoints <- "roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv"
total_distance <- 0
next_distance <- \(waypoints, to) {
located <- str_locate(str_sub(waypoints, 2), to)
if (dim(located)[1] == 1) {
distance <- unname(located[1, 2])
}
ifelse(is.na(distance), 0, distance)
}
next_waypoints <- \(waypoints, distance) {
str_sub(waypoints, distance + 1)
}
for (to in chars("obbvb")) {
distance <- next_distance(waypoints, to)
waypoints <- next_waypoints(waypoints, distance)
total_distance <- total_distance + distance
}
result_example1 <- total_distance
assertthat::are_equal(expect_example1, result_example1)
## [1] TRUE
expect_example2 <- 11L
expect_example3 <- 30L
expect_example4 <- 35L
compute1 <- \(df) {
waypoints <- paste0(
rep("roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv", 200),
collapse = ""
)
total_distance <- 0
program <- df$x
for (to in program) {
distance <- next_distance(waypoints, to)
waypoints <- next_waypoints(waypoints, distance)
total_distance <- total_distance + distance
}
total_distance
}
result_example1 <- example1 |>
compute1()
assertthat::are_equal(expect_example1, result_example1)
## [1] TRUE
example2 <- tibble(x = chars("roygbv"))
result_example2 <- example2 |>
compute1()
assertthat::are_equal(expect_example2, result_example2)
## [1] TRUE
example3 <- tibble(x = chars("vbgyor"))
result_example3 <- example3 |>
compute1()
assertthat::are_equal(expect_example3, result_example3)
## [1] TRUE
example4 <- tibble(x = chars("vvvvvv"))
result_example4 <- example4 |>
compute1()
assertthat::are_equal(expect_example4, result_example4)
## [1] TRUE
input <- adventofcode_input(input_year, input_day, what = "character")
df <- tibble(x = chars(input))
result_part1 <- df |>
compute1()
result_part1
## [1] 3604
expect_part2_example1 <- 32L
part2_example1 <- tibble(x = str_split_1("rybryb", ""))
waypoints <- paste0(
rep("roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv", 200),
collapse = ""
)
total_distance <- 0
furthest_distance <- \(waypoints, program) {
distances <- map_dbl(unique(program), \(x) next_distance(waypoints, x))
max_distance <- ifelse(length(distances), max(distances), 0)
list(to = which.max(distances), distance = max_distance)
}
next_program <- \(program, to) program[-to]
compute2 <- \(df) {
steps <- 200
waypoints <- paste0(
rep("roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv", steps),
collapse = ""
)
total_distance <- 0
program <- df$x
furthest <- furthest_distance(waypoints, program)
to <- furthest$to
distance <- furthest$distance
while (length(to) > 0) {
waypoints <- next_waypoints(waypoints, distance)
total_distance <- total_distance + distance
program <- next_program(program, to)
furthest <- furthest_distance(waypoints, program)
to <- furthest$to
distance <- furthest$distance
}
total_distance
}
result_part2_example1 <- part2_example1 |>
compute2()
assertthat::are_equal(expect_part2_example1, result_part2_example1)
## [1] TRUE
expect_example2 <- 33L
example2 <- tibble(x = chars("vrvbvg"))
result_example2 <- example2 |>
compute2()
assertthat::are_equal(expect_example2, result_example2)
## [1] TRUE
expect_example3 <- 55L
example3 <- tibble(x = chars("groovyyoyo"))
result_example3 <- example3 |>
compute2()
assertthat::are_equal(expect_example3, result_example3)
## [1] TRUE
expect_example4 <- 103L
example4 <- tibble(x = chars("roygbvroygbvroygbv"))
result_example4 <- example4 |>
compute2()
assertthat::are_equal(expect_example4, result_example4)
## [1] TRUE
df <- tibble(x = chars(input))
result_part2 <- df |>
compute2()
result_part2
## [1] 5965