Task 1

Thorn Thaler - <

2025-04-12

1 Setup

1.1 Libraries

library(httr)
library(xml2)
library(magrittr)
library(dplyr)
library(purrr)
library(stringr)

1.2 Retrieve Data from AoC

session_cookie <- set_cookies(session = keyring::key_get("AoC-GitHub-Cookie"))
base_url <- paste0("https://adventofcode.com/", params$year, "/day/", params$task_nr)
puzzle <- GET(base_url,
              session_cookie) %>% 
  content(encoding = "UTF-8") %>% 
  xml_find_all("///article") %>% 
  lapply(as.character)

parse_puzzle_data <- function(text_block = readClipboard()) {
  if (length(text_block) == 1L) {
    text_block <- text_block %>% 
      str_split("\n") %>% 
      extract2(1L) %>% 
      keep(nzchar)
  }
  text_block %>% 
    str_split(", ?") %>% 
    extract2(1L)
}

puzzle_data <- local({
  GET(paste0(base_url, "/input"),
      session_cookie) %>% 
    content(encoding = "UTF-8") %>% 
    parse_puzzle_data()
})

2 Puzzle Day 1

2.1 Part 1

2.1.1 Description

— Day 1: No Time for a Taxicab —

Santa’s sleigh uses a very high-precision clock to guide its movements, and the clock’s oscillator is regulated by stars. Unfortunately, the stars have been stolen… by the Easter Bunny. To save Christmas, Santa needs you to retrieve all fifty stars by December 25th.

Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

You’re airdropped near Easter Bunny Headquarters in a city somewhere. “Near”, unfortunately, is as close as you can get - the instructions on the Easter Bunny Recruiting Document the Elves intercepted start here, and nobody had time to work them out further.

The Document indicates that you should start at the given coordinates (where you just landed) and face North. Then, follow the provided sequence: either turn left (L) or right (R) 90 degrees, then walk forward the given number of blocks, ending at a new intersection.

There’s no time to follow such ridiculous instructions on foot, though, so you take a moment and work out the destination. Given that you can only walk on the street grid of the city, how far is the shortest path to the destination?

For example:

  • Following R2, L3 leaves you 2 blocks East and 3 blocks North, or 5 blocks away.
  • R2, R2, R2 leaves you 2 blocks due South of your starting position, which is 2 blocks away.
  • R5, L5, R5, R3 leaves you 12 blocks away.

How many blocks away is Easter Bunny HQ?

2.1.2 Solution

First, we calculate the endpoint, then we calculate the manhatten distance to get the result.

get_path <- function(ops) {
  accumulate(ops, function(pos, op) {
    op <- str_extract_all(op, "[LR]|\\d+") %>% 
      extract2(1L)
    new_dir <- case_when(
      pos$dir == "N" ~ if_else(op[1L] == "L", "W", "E"),
      pos$dir == "E" ~ if_else(op[1L] == "L", "N", "S"),
      pos$dir == "S" ~ if_else(op[1L] == "L", "E", "W"),
      pos$dir == "W" ~ if_else(op[1L] == "L", "S", "N")
    )
    new_pos <- list(
      N = c(0L, -1L),
      E = c(1L, 0L),
      S = c(0L, 1L),
      W = c(-1L, 0L)
    )[[new_dir]] * as.integer(op[2L]) + pos$pos
    list(dir = new_dir, pos = new_pos)
  }, .init = list(dir = "N", pos = c(0L, 0L)))
}

path <- get_path(puzzle_data)
sum(abs(tail(path, 1L)[[1L]]$pos))
## [1] 253

2.2 Part 2

2.2.1 Description

— Part Two —

Then, you notice the instructions continue on the back of the Recruiting Document. Easter Bunny HQ is actually at the first location you visit twice.

For example, if your instructions are R8, R4, R4, R8, the first location you visit twice is 4 blocks away, due East.

How many blocks away is the first location you visit twice?

2.2.2 Solution

We simply look at all the positions, fill in the intermediate steps and check which point was visited twice first.

get_intermediate_steps <- function(all_pos) {
  intermediate_pos <- matrix(0, 0L, 2L)
  for (i in 1:(nrow(all_pos) - 1L)) {
    start <- all_pos[i, ]
    end <- all_pos[i + 1L,]
    if (start[1L] == end[1L]) {
      intermediate_pos <- rbind(intermediate_pos,
                                cbind(start[1L], start[2L]:end[2L]))
    } else {
      intermediate_pos <- rbind(intermediate_pos,
                                cbind(start[1L]:end[1L], start[2L]))
    }
    # Remove end point as it will be included in the next iteration
    intermediate_pos <- intermediate_pos %>% 
      head(-1L)
  }
  intermediate_pos
}

get_first_duplicated <- function(path) {
  all_pos <- map(path, "pos") %>% 
    do.call(rbind, .) 
  int_pos <- get_intermediate_steps(all_pos)
  idx <- int_pos %>% 
    duplicated() %>% 
    which() %>% 
    min()
  int_pos[idx, ]
}

sum(abs(get_first_duplicated(path)))
## [1] 126