Advent of Code Setup

Setup
Author

Francisco Sánchez-Sáez

Published

June 26, 2024

Introduction

In the Advent of Code (AoC) puzzles, the puzzle input is different for each user. In this entry, we will set up our session to load the input through our R Session. The instructions and functions are inspired by the https://github.com/botan/aocr?tab=readme-ov-file and https://github.com/dgrtwo/adventdrob/blob/main/R/input.R repositories.

Load packages and secrets.R File

We will use the httr2 package to get the inputs. First of all, we need to get the session information. We need to do the following steps:

  1. Log in to https://adventofcode.com/ using your preferred web browser.

  2. Press F12 or right click on the web page and click “Inspect” to open the developer console.

  3. Go the Network tab and reload the page.

  4. Click on the “Advent of Code” request at the top left of the screen.

  5. Go to the “Cookie” tab under the request” “Headers”.

  6. You will find your cookie in session=. The cookie session is like a password, so you don’t want to put it with the code, that could be shown to other people so you need to keep it secret. The main options are to store it in a .Renviron file, which will be ready to load every time you start a session of R on this computer, or in a separate file (e.g. “secrets.R”).

  7. Copy and paste this cookie session into your .Renviron as AOC_SESSION=. If you have any trouble with finding your .Renviron file, you can use usethis::edit_r_environ(). Restart R session.

# Load packages--------------------------------------------------------------
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(httr2)
# Copy the cookie session and paste it into .Renviron
# usethis::edit_r_environ()

# Alternatively source secrets.R
# source("..", "..", "secrets.R")

Configuration of the session

Once we have stored the cookie session we create a function that allows us to retrieve the input selecting the year and the day.

# Create a function to read the inputs---------------------------------------
read_aoc_input <- function(
                  year,
                  day ,
                  verbose = TRUE,
                  open_file = FALSE) {
  
  session <- paste0("session=", Sys.getenv("AOC_SESSION"))
  if (session == "session=") {
    rlang::abort(c(
      "AOC_SESSION not found, please set the session in .Renviron or in secrets.R",
    ))
  }
  
  url <- paste0(
    "https://adventofcode.com/",
    year,
    "/day/",
    day,
    "/input"
  )

  if (nchar(day) == 1) day <- paste0(0, day)
  req <- httr2::request(url)
  req <- httr2::req_headers(req, cookie = session)
  response <- httr2::req_perform(req)
  if (resp_status(response) == 200) {
  # Get the content of the response
  puzzle_input <- resp_body_string(response)
  # Print the puzzle input
} else {
  # Print debug information about the response
  cat("Failed to fetch the puzzle input. Status code:", resp_status(response), "\n")
  cat("Response Headers:\n")
  print(response$headers)
  cat("Response Body:\n")
  print(resp_body_string(response))
}
}
# Check the function to read the inputs--------------------------------------
input <- read_aoc_input(year = 2015, day = 1)

Convert into a working format

# Convert the input to a working formats--------------------------------------
input_lines <- str_split(input, "\n")[[1]]
input_tibble <- tibble(x = input_lines)