Press F12 or right click on the web page and click “Inspect” to open the developer console.
Go the Network tab and reload the page.
Click on the “Advent of Code” request at the top left of the screen.
Go to the “Cookie” tab under the request” “Headers”.
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”).
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.
── 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 responsecat("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)
Start the search
Now that we have everything ready, we can start searching the stars. I’m not going to post the solutions in chronological order, but in order of difficulty, so first I will post 2015 day 1, then 2016 day 1, …, until 2023 day 1. Then the days 2 and so on. Stay tuned!