| Title: | Companion to Advent of Code and Associated Personal Website |
|---|---|
| Description: | Use with advent-of-code-website-template to create a website for Advent of Code solutions. Also includes functions for getting and reading in puzzle input. |
| Authors: | Ella Kaye [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-7300-3718>) |
| Maintainer: | Ella Kaye <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0.9000 |
| Built: | 2026-06-05 06:09:30 UTC |
| Source: | https://github.com/EllaKaye/aochelpers |
Delete the introduction post with relative path ./YYYY/day/conclusion (where YYYY
is the value of year), if it exists.
aoc_delete_conclusion(year = NULL)aoc_delete_conclusion(year = NULL)
year |
An integer representing the year, from 2015 to the current year. Defaults to the current year. |
## Not run: aoc_delete_conclusion(2022)## Not run: aoc_delete_conclusion(2022)
Assumes that the directory for the year has the relative path "./YYYY" and
the directory for the day has the relative path "./YYYY/day/DD", where
YYYY and DD are the values of year and day. This will be the case if
the post was created using aoc_new_day(). If the directory for the day or
year has a corresponding directory in "./_freeze", that will also be
deleted. Additionally, for aoc_delete_year(), the listing page ./YYYY.qmd
(where YYYY is the value of year) will be deleted.
aoc_delete_day(day, year = NULL) aoc_delete_year(year = NULL)aoc_delete_day(day, year = NULL) aoc_delete_year(year = NULL)
day |
An integer between 1 and 25 |
year |
An integer representing the year, from 2015 to the current year. Defaults to the current year. |
The path of the day that was deleted (invisibly)
## Not run: aoc_delete_day(1, 2022) ## Not run: aoc_delete_year(2022)## Not run: aoc_delete_day(1, 2022) ## Not run: aoc_delete_year(2022)
Delete the introduction post with relative path ./YYYY/day/introduction (where YYYY
is the value of year), if it exists.
aoc_delete_intro(year = NULL)aoc_delete_intro(year = NULL)
year |
An integer representing the year, from 2015 to the current year. Defaults to the current year. |
aoc_new_intro() aoc_new_year() aoc_delete_year()
## Not run: aoc_delete_intro(2022)## Not run: aoc_delete_intro(2022)
Get the puzzle input from the Advent of Code website for a given day and year
and save it to a file. The file will be saved in the current working
directory with a relative path "./YYYY/day/DD/input", where YYYY is the
value of the year parameter and DD is value of the day parameter. This path
echoes the URL structure of the Advent of Code website.
aoc_get_input(day, year = NULL)aoc_get_input(day, year = NULL)
day |
An integer between 1 and 25 |
year |
An integer representing the year, from 2015 to the current year. Defaults to the current year. |
This function assumes that you have an account on the Advent of Code website. For this function to work, you must set
the ADVENT_SESSION environment variable in your .Renviron file. For
guidance on how to find your session token, see
https://github.com/dgrtwo/adventdrob/tree/main#installation. Once you have
your session token, you can set the environment variable with
usethis::edit_r_environ(). This function is adapted from
https://github.com/dgrtwo/adventdrob/blob/main/R/input.R, but saves the
input to a file instead of returning it as a data frame. Once the input is
saved, it can be read in with aoc_input_vector(), aoc_input_data_frame() or
aoc_input_matrix(), whichever is appropriate for the puzzle.
This function is also called as part of aoc_new_day().
Returns, invisibly, a character string with the absolute path to the input file.
aoc_new_day() aoc_input_vector() aoc_input_data_frame() [aoc_input_matrix()
## Not run: aoc_get_input(1, 2022)## Not run: aoc_get_input(1, 2022)
Read in the puzzle input, or other file, as a data frame, one row per line.
It assumes that the file is stored in the directory
"./YYYY/day/DD", where YYYY and DD are the values of year and day.
By default, the file name is "input".
This file will exist in this location if the post was created using aoc_new_day().
class is tbl_df, the reading is done by readr::read_table(). If the class
is data.frame, the reading is done by utils::read.table().
The defaults have been chosen to suit typical Advent of Code puzzle input.
aoc_input_data_frame( day, year = NULL, file = "input", class = c("tbl_df", "data.frame"), col_names = FALSE, show_col_types = FALSE, view = FALSE )aoc_input_data_frame( day, year = NULL, file = "input", class = c("tbl_df", "data.frame"), col_names = FALSE, show_col_types = FALSE, view = FALSE )
day |
An integer between 1 and 25 |
year |
An integer representing the year, from 2015 to the current year. Defaults to the current year. |
file |
Character string. The name of a file that exists in the directory
|
class |
character. One of 'tbl_df' or 'data.frame'. Default is 'tbl_df'. |
col_names |
logical. Only relevant if class is |
show_col_types |
logical. If |
view |
logical. If |
A data frame containing the puzzle input for the day and year.
aoc_input_vector() aoc_input_matrix()
## Not run: aoc_input_data_frame(8, 2020)## Not run: aoc_input_data_frame(8, 2020)
Read in the puzzle input, or other file, as a matrix, one row per line, by
default one character per column. It assumes that the file is stored in the
directory "./YYYY/day/DD", where YYYY and DD are the values of year
and day. By default, the file name is "input". This file will exist in
this location if the post was created using aoc_new_day(). The initial
reading is done by readLines().
aoc_input_matrix( day, year = NULL, file = "input", mode = c("character", "numeric"), split = "", view = FALSE )aoc_input_matrix( day, year = NULL, file = "input", mode = c("character", "numeric"), split = "", view = FALSE )
day |
An integer between 1 and 25 |
year |
An integer representing the year, from 2015 to the current year. Defaults to the current year. |
file |
Character string. The name of a file that exists in the directory
|
mode |
Character string. One of 'character' or 'numeric'. Default is 'character'. |
split |
character. The string to split the input on. Default is |
view |
logical. If |
A matrix containing the puzzle input for the day and year.
aoc_input_vector() aoc_input_data_frame()
## Not run: aoc_input_matrix(3, 2020)## Not run: aoc_input_matrix(3, 2020)
Read in the puzzle input, or other file, as a vector, one element per line.
It assumes that the file is stored in the directory
"./YYYY/day/DD", where YYYY and DD are the values of year and day.
By default, the file name is "input".
This file will exist in this location if the post was created using aoc_new_day(). The
reading is done by readLines().
aoc_input_vector( day, year = NULL, file = "input", mode = c("character", "numeric") )aoc_input_vector( day, year = NULL, file = "input", mode = c("character", "numeric") )
day |
An integer between 1 and 25 |
year |
An integer representing the year, from 2015 to the current year. Defaults to the current year. |
file |
Character string. The name of a file that exists in the directory
|
mode |
Character string. One of 'character' or 'numeric'. Default is 'character'. |
A vector containing the puzzle input for the day and year.
aoc_input_data_frame() aoc_input_matrix()
## Not run: aoc_input_vector(1, 2020, "numeric")## Not run: aoc_input_vector(1, 2020, "numeric")
Create a conclusion post with relative path ./YYYY/day/conclusion (where YYYY
is the value of year) by copying the template at ./_templates/YYYY-conclusion
and substituting YYYY for the value of year, both in the new file name and in the
file itself.
aoc_new_conclusion(year = NULL)aoc_new_conclusion(year = NULL)
year |
An integer representing the year, from 2015 to the current year. Defaults to the current year. |
The path to the conclusion post (invisibly)
## Not run: aoc_new_introduction(2022)## Not run: aoc_new_introduction(2022)
Create the necessary directories for a new post, copy in template files and make them relevant to the day and year (see Details).
aoc_new_day(day, year = NULL) aoc_new_post(day, year = NULL)aoc_new_day(day, year = NULL) aoc_new_post(day, year = NULL)
day |
An integer between 1 and 25 |
year |
An integer representing the year, from 2015 to the current year. Defaults to the current year. |
aoc_new_post() and
aoc_new_day() both assume they being called from a project with a directory
_templates, with a subdirectory post-template that contains, at minimum,
the file index.qmd. They will copy all the files in post-template into a
directory "./YYYY/day/DD" where YYYY is the value of the year parameter and
DD is value of the day parameter (creating these directories if they do not
already exist). This path echoes the URL structure of the Advent of Code
website. Additionally, they replace any instances of YYYY and DD in the
index.qmd and (if present) script.R files with the values of the year and day
parameters, respectively. In addition, aoc_new_day() will also run
aoc_get_input() to download the puzzle input and save it into the post
directory.
If you have your Advent of Code session token set in your
.Renviron file, we recommend using aoc_new_day() over aoc_new_post().
If you wish to download and save your puzzle input separately, use
aoc_new_post().
The path to the new day (invisibly)
## Not run: aoc_new_day(1, 2022) ## Not run: aoc_new_post(1, 2022)## Not run: aoc_new_day(1, 2022) ## Not run: aoc_new_post(1, 2022)
Create an introduction post with relative path ./YYYY/day/introduction (where YYYY
is the value of year) by copying the template at ./_templates/YYYY-intro
and substituting YYYY for the value of year, both in the new file name and in the
file itself.
aoc_new_intro(year = NULL)aoc_new_intro(year = NULL)
year |
An integer representing the year, from 2015 to the current year. Defaults to the current year. |
The path to the introduction post (invisibly)
aoc_delete_intro() aoc_new_year()
## Not run: aoc_new_intro(2022)## Not run: aoc_new_intro(2022)
Creates a directory with relative path ./YYYY (where YYYY is the value of
year) and copies the listing template at "./_templates/YYYY.qmd" to
YYYY.qmd and, for the latter, replaces YYYY with the value of year,
both in the file name and in the file itself. Additionally, an introduction
post and a _metadata.yml may be created. See Details for more information.
aoc_new_year(year = NULL, intro = TRUE)aoc_new_year(year = NULL, intro = TRUE)
year |
An integer representing the year, from 2015 to the current year. Defaults to the current year. |
intro |
Logical. Whether to include an introduction post. |
The listing page, ./YYYY.qmd (where YYYY is the value of year) picks up
posts which are in the subdirectory ./YYYY/day (which echoes the URL
structure of the Advent of Code website). Note that the website will fail to
render if there are no posts under the day directory. To avoid that
problem, by default an introduction post introduction is created, assuming
that the directory ./_templates/YYYY-intro exists (which it does in the
website template
https://github.com/EllaKaye/advent-of-code-website-template.) If you don't
want an introduction post, set intro = FALSE. An introduction post can also
be created separately with aoc_new_intro() or deleted with
aoc_delete_intro(). Without an introduction post, note that you will need
at least one other post, e.g. through a call to aoc_new_day() before
rendering the website.
Additionally, if there is a file ./_templates/_metadata.yml, this will be
copied into ./YYYY/day/_metadata.yml. This file is used to set the metadata
only for the posts in the day directory.
If there is no intro post and no _metadata.yml file, then only the
"./YYYY" directory will be created, not the subdirectory ./YYYY/day. The
latter will then be created on the first call to aoc_new_day() for that
year. In this case, the website will render after a call to aoc_new_year().
The path to the new year directory (invisibly)
aoc_new_day(), aoc_new_intro(), aoc_delete_intro(),
aoc_delete_year()
## Not run: aoc_new_year(2022)## Not run: aoc_new_year(2022)
Get the URL from the Advent of Code website for the puzzle and the puzzle input for a given day and year.
aoc_url(day, year = NULL, browse = FALSE) aoc_url_input(day, year = NULL, browse = FALSE)aoc_url(day, year = NULL, browse = FALSE) aoc_url_input(day, year = NULL, browse = FALSE)
day |
An integer between 1 and 25 |
year |
An integer representing the year, from 2015 to the current year. Defaults to the current year. |
browse |
logical - should the URL be opened in the browser? |
A character string with the URL
aoc_url(1, 2022) aoc_url_input(1, 2022)aoc_url(1, 2022) aoc_url_input(1, 2022)
Extract all numbers from a string
extract_numbers(x)extract_numbers(x)
x |
character. The string to extract numbers from. |
A numeric vector containing all numbers in the string.
extract_numbers("abc123def456") extract_numbers("Game 1:") extract_numbers("Cards: 1 3 16 136") extract_numbers("1, -3, 16, -136")extract_numbers("abc123def456") extract_numbers("Game 1:") extract_numbers("Cards: 1 3 16 136") extract_numbers("1, -3, 16, -136")
Greatest Common Divisor (GCD) and Least Common Multiple (LCM)
GCD(x, y) LCM(x, y)GCD(x, y) LCM(x, y)
x |
A single integer |
y |
A single integer |
The greatest common divisor of x and y
GCD(12, 18) GCD(12, 0) GCD(13, 2) LCM(12, 18) LCM(2, 6) LCM(3, 5)GCD(12, 18) GCD(12, 0) GCD(13, 2) LCM(12, 18) LCM(2, 6) LCM(3, 5)
For the default split of ", assumes that each element of lines has the same number of characters.
lines_to_matrix(lines, split = "")lines_to_matrix(lines, split = "")
lines |
a vector |
split |
character. The string to split the input on. Default is |
A matrix where the number of rows is the length of lines and, for a split on ", the number of columns is the same as the number of characters in each element of lines.
lines_to_matrix(c("#.#.", "..#.", "##.."))lines_to_matrix(c("#.#.", "..#.", "##.."))
When the strings represent numbers, it's better to read them in as a character vector using aoc_input_vector(), then change mode to numeric as part of call to split_at_blanks().
split_at_blanks(lines, mode = c("character", "numeric"), split = "")split_at_blanks(lines, mode = c("character", "numeric"), split = "")
lines |
A character vector |
mode |
Character string. One of 'character' or 'numeric'. Default is 'character'. |
split |
character. The string to split the input on. Default is |
A list, with one element per group originally separated by blank lines.
x <- c("123", "1234", "", "56", "567", "", "89") split_at_blanks(x) split_at_blanks(x, "numeric")x <- c("123", "1234", "", "56", "567", "", "89") split_at_blanks(x) split_at_blanks(x, "numeric")