Caio Lente

Advent of R with aor

ยท Caio Lente

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:

# install.packages("devtools")
devtools::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:

# Start the puzzle for 2022-12-01 in the aoc2022/ directory
aor::day_start("2022-12-01", "aoc2022/")
#> โœ” Fetched title.
#> โœ” Fetched puzzle.
#> โœ” Fetched input.

# 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.

And here is what the file looks like after runing 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!

#r #aoc #pkg

Reply to this post by email โ†ช