Code Monkey home page Code Monkey logo

aor's Introduction

aor

Lifecycle: experimental R-CMD-check

The goal of aor is to help useRs solve the Advent of Code in R! It has some helper functions to fetch puzzles and inputs as quickly as possible.

Installation

You can install the development version of aor from GitHub with:

# install.packages("devtools")
devtools::install_github("clente/aor")

aor also calls pandoc internally. If you don’t use RStudio, you might have to install pandoc manually before using this package.

Authentication

Before using aor, you’ll probably want to save your Advent of Code session cookie as an environment variable; this is necessary for aor to fetch the 2nd part of puzzles.

In order to find your session cookie, do the following:

  1. Navigate to https://adventofcode.com/.
  2. Press F12 to open your browser’s developer tools.
  3. Select the Network tab.
  4. Reload the page.
  5. Click on the first request that shows up.
  6. In Request Headers, copy the long string after session=.

Once you have your session cookie, add it to an environment variable called AOC_SESSION. You can do this by running the following command:

# Add your cookie to .Renviron
readr::write_lines('AOC_SESSION="YOUR_COOKIE"', "~/.Renviron", append = TRUE)

If this doesn’t work even after restarting R, your can force your session cookie directly with the following command (just remember to never upload your cookie to GitHub):

# Set the environment variable directly
Sys.setenv("AOC_SESSION" = "YOUR_COOKIE")

Example

The basic usage of aor revolves around day_start() and day_continue(). By default, both functions fetch the current day’s puzzle, but I’ll use 2022-12-01 as the example here:

# Start the puzzle for 2022-12-01 in the aoc2022/ directory
aor::day_start("2022-12-01", "aoc2022/")
#> ✔ Fetched puzzle.
#> ✔ Fetched input.
#> ✔ Created directory 'aoc2022/01_calorie_counting'
#> ✔ Wrote part 1 to 'aoc2022/01_calorie_counting/puzzle.R'
#> ✔ Wrote input to 'aoc2022/01_calorie_counting/input.txt'
#> ℹ To fetch part 2, run `aor::day_continue("2022-12-01", "aoc2022/01_calorie_counting/puzzle.R")`

# Files created
fs::dir_tree("aoc2022/")
#> aoc2022/
#> └── 01_calorie_counting
#>     ├── input.txt
#>     └── puzzle.R

Here is what aoc2022/01_calorie_counting/puzzle.R looks like (note that I’m omitting most of the question so that the output is not super long):

# --- Day 1: Calorie Counting ---
#
# Santa's reindeer typically eat regular reindeer food, but they need a
# lot of [magical energy](/2018/day/25) to deliver presents on Christmas.
# For that, their favorite snack is a special type of *star* fruit that
# only grows deep in the jungle. The Elves have brought you on their
# annual expedition to the grove where the fruit grows.
#
# ...
#
# Find the Elf carrying the most Calories. *How many total Calories is
# that Elf carrying?*

# Your input can be found on the file below:
input <- "aoc2022/01_calorie_counting/input.txt"

# Once you're done with part 1, run the following line to fetch part 2:
aor::day_continue("2022-12-01", "aoc2022/01_calorie_counting/puzzle.R")

Once you solve part 1 of the puzzle, you can run the last line of the template to automatically fetch part 2 into the same file!

aor::day_continue("2022-12-01", "aoc2022/01_calorie_counting/puzzle.R")
#> ✔ Fetched puzzle.
#> ✔ Wrote part 2 to 'aoc2022/01_calorie_counting/puzzle.R'

And here is what the file looks like after running day_continue() (again omitting most of the question):

# --- Day 1: Calorie Counting ---
#
# Santa's reindeer typically eat regular reindeer food, but they need a
# lot of [magical energy](/2018/day/25) to deliver presents on Christmas.
# For that, their favorite snack is a special type of *star* fruit that
# only grows deep in the jungle. The Elves have brought you on their
# annual expedition to the grove where the fruit grows.
#
# ...
#
# Find the Elf carrying the most Calories. *How many total Calories is
# that Elf carrying?*

# Your input can be found on the file below:
input <- "aoc2022/01_calorie_counting/input.txt"

# Once you're done with part 1, run the following line to fetch part 2:
aor::day_continue("2022-12-01", "aoc2022/01_calorie_counting/puzzle.R")

# --- Part Two ---
#
# By the time you calculate the answer to the Elves' question, they've
# already realized that the Elf carrying the most Calories of food might
# eventually *run out of snacks*.
#
# ...
#
# Find the top three Elves carrying the most Calories. *How many Calories
# are those Elves carrying in total?*

Good luck! See you on the other side!

Ethics

The source code of the Advent of Code has a message for us:

Please be careful with automated requests; I’m not a massive company, and I can only take so much traffic. Please be considerate so that everyone gets to play.

With this in mind, aor makes exactly the same number of requests as a regular user would: one to get part 1 of the puzzle, one to get the input and one to get part 2. Even aor’s unit tests are mocked!

Having said this, please refrain from making spurious requests using aor; don’t create bots, scrapers, or anything of the like. Thanks for your comprehension.

aor's People

Contributors

clente avatar hulacst avatar

Stargazers

Tan Ho avatar Aurélien Ginolhac avatar  avatar Mattias avatar

Watchers

 avatar

aor's Issues

day_continue fails

aor::day_continue("2022-12-02", "aoc2022/02_rock_paper_scissors/puzzle.R")

gives:

Error in UseMethod("xml_find_all") : 
  no applicable method for 'xml_find_all' applied to an object of class "NULL"
Error: [ENOENT] Failed to remove '/var/folders/3v/9bfvrwnn0gn50mfplb8xcchr0000gn/T/Rtmp9ump1s/fileb4c02836e3dc.html': no such file or directory

Working with the debugger, I find that the result of the httr GET request for the article tags is a list of length 1: the contents of the first article tag for the Part One puzzle. Hence purrr:pluck(part) gives NULL. (Two article tags are present when I view the site with the browser inspector, but that's after I solved the second problem.)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.