Caio Lente

Advent of R with {aor}

If you follow me on social media, you might remember that last year I wrote a post (in Portuguese) about solving every puzzle of 2021’s Advent of Code in R. This year I’m back to the party, but this time with a gift for everybody 🎁

The event

If you’re not in the know, the Advent of Code is an Advent Calendar for programmers. Between December 1st and Christmas of every year, the organizers post 1 programming puzzle per day and the goal is to get to December 25th having solved all 25 puzzles.

Last year, in what I called the Advent of R, I challenged myself to actually try and solve each puzzle on the day they were posted, in R and documenting the whole process. I was able to do it, but it was extremely difficult! This year I’ll take it easy and solve the puzzles at my own pace.

But I didn’t want to leave the R community empty-handed… In 2022, my contribution to the Advent of R is a package to help the whole community πŸŽ‰

The {aor} package

The goal of {aor} is to help R developers who are trying to solve the Advent of Code using this amazing programming language. 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:

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

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:

 1# Start the puzzle for 2022-12-01 in the aoc2022/ directory
 2aor::day_start("2022-12-01", "aoc2022/")
 3#> βœ” Fetched title.
 4#> βœ” Fetched puzzle.
 5#> βœ” Fetched input.
 6
 7# Files created
 8fs::dir_tree("aoc2022/")
 9#> aoc2022/
10#> └── 01_calorie_counting
11#>     β”œβ”€β”€ input.txt
12#>     └── 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):

 1# --- Day 1: Calorie Counting ---
 2#
 3# Santa's reindeer typically eat regular reindeer food, but they need a
 4# lot of [magical energy](/2018/day/25) to deliver presents on Christmas.
 5# For that, their favorite snack is a special type of *star* fruit that
 6# only grows deep in the jungle. The Elves have brought you on their
 7# annual expedition to the grove where the fruit grows.
 8#
 9# ...
10#
11# Find the Elf carrying the most Calories. *How many total Calories is
12# that Elf carrying?*
13
14# Your input can be found on the file below:
15input <- "aoc2022/01_calorie_counting/input.txt"
16
17# Once you're done with part 1, run the following line to fetch part 2:
18aor::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!

1aor::day_continue("2022-12-01", "aoc2022/01_calorie_counting/puzzle.R")
2#> βœ” Fetched puzzle.

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

 1# --- Day 1: Calorie Counting ---
 2#
 3# Santa's reindeer typically eat regular reindeer food, but they need a
 4# lot of [magical energy](/2018/day/25) to deliver presents on Christmas.
 5# For that, their favorite snack is a special type of *star* fruit that
 6# only grows deep in the jungle. The Elves have brought you on their
 7# annual expedition to the grove where the fruit grows.
 8#
 9# ...
10#
11# Find the Elf carrying the most Calories. *How many total Calories is
12# that Elf carrying?*
13
14# Your input can be found on the file below:
15input <- "aoc2022/01_calorie_counting/input.txt"
16
17# Once you're done with part 1, run the following line to fetch part 2:
18aor::day_continue("2022-12-01", "aoc2022/01_calorie_counting/puzzle.R")
19
20# --- Part Two ---
21#
22# By the time you calculate the answer to the Elves' question, they've
23# already realized that the Elf carrying the most Calories of food might
24# eventually *run out of snacks*.
25#
26# ...
27#
28# Find the top three Elves carrying the most Calories. *How many Calories
29# are those Elves carrying in total?*

Good luck! See you on the other side!

#r   #aoc   #pkg