Package 'aochelpers'

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

Help Index


Delete a conclusion post

Description

Delete the introduction post with relative path ./YYYY/day/conclusion (where YYYY is the value of year), if it exists.

Usage

aoc_delete_conclusion(year = NULL)

Arguments

year

An integer representing the year, from 2015 to the current year. Defaults to the current year.

See Also

aoc_new_conclusion()

Examples

## Not run: aoc_delete_conclusion(2022)

Delete the directory for a day or year

Description

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.

Usage

aoc_delete_day(day, year = NULL)

aoc_delete_year(year = NULL)

Arguments

day

An integer between 1 and 25

year

An integer representing the year, from 2015 to the current year. Defaults to the current year.

Value

The path of the day that was deleted (invisibly)

Examples

## Not run: aoc_delete_day(1, 2022)
## Not run: aoc_delete_year(2022)

Delete an introduction post

Description

Delete the introduction post with relative path ./YYYY/day/introduction (where YYYY is the value of year), if it exists.

Usage

aoc_delete_intro(year = NULL)

Arguments

year

An integer representing the year, from 2015 to the current year. Defaults to the current year.

See Also

aoc_new_intro() aoc_new_year() aoc_delete_year()

Examples

## Not run: aoc_delete_intro(2022)

Get and save the puzzle input

Description

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.

Usage

aoc_get_input(day, year = NULL)

Arguments

day

An integer between 1 and 25

year

An integer representing the year, from 2015 to the current year. Defaults to the current year.

Details

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().

Value

Returns, invisibly, a character string with the absolute path to the input file.

See Also

aoc_new_day() aoc_input_vector() aoc_input_data_frame() [aoc_input_matrix()

Examples

## Not run: aoc_get_input(1, 2022)

Read puzzle input as a data frame

Description

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.

Usage

aoc_input_data_frame(
  day,
  year = NULL,
  file = "input",
  class = c("tbl_df", "data.frame"),
  col_names = FALSE,
  show_col_types = FALSE,
  view = FALSE
)

Arguments

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 "./YYYY/day/DD", where YYYY and DD are the values of year and day. Defaults to "input".

class

character. One of 'tbl_df' or 'data.frame'. Default is 'tbl_df'.

col_names

logical. Only relevant if class is tbl_df. If TRUE, the first row of the input file will be used as the column names. Default is FALSE

show_col_types

logical. If TRUE, the column types will be shown. Default is FALSE

view

logical. If TRUE, calls utils::View() to view the returned input. Default is FALSE

Value

A data frame containing the puzzle input for the day and year.

See Also

aoc_input_vector() aoc_input_matrix()

Examples

## Not run: aoc_input_data_frame(8, 2020)

Read puzzle input as a matrix

Description

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().

Usage

aoc_input_matrix(
  day,
  year = NULL,
  file = "input",
  mode = c("character", "numeric"),
  split = "",
  view = FALSE
)

Arguments

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 "./YYYY/day/DD", where YYYY and DD are the values of year and day. Defaults to "input".

mode

Character string. One of 'character' or 'numeric'. Default is 'character'.

split

character. The string to split the input on. Default is "", i.e. one character per column

view

logical. If TRUE, calls utils::View() to view the returned input. Default is FALSE

Value

A matrix containing the puzzle input for the day and year.

See Also

aoc_input_vector() aoc_input_data_frame()

Examples

## Not run: aoc_input_matrix(3, 2020)

Read puzzle input as a vector

Description

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().

Usage

aoc_input_vector(
  day,
  year = NULL,
  file = "input",
  mode = c("character", "numeric")
)

Arguments

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 "./YYYY/day/DD", where YYYY and DD are the values of year and day. Defaults to "input".

mode

Character string. One of 'character' or 'numeric'. Default is 'character'.

Value

A vector containing the puzzle input for the day and year.

See Also

aoc_input_data_frame() aoc_input_matrix()

Examples

## Not run: aoc_input_vector(1, 2020, "numeric")

Create a conclusion post

Description

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.

Usage

aoc_new_conclusion(year = NULL)

Arguments

year

An integer representing the year, from 2015 to the current year. Defaults to the current year.

Value

The path to the conclusion post (invisibly)

See Also

aoc_delete_conclusion()

Examples

## Not run: aoc_new_introduction(2022)

Set up a new day

Description

Create the necessary directories for a new post, copy in template files and make them relevant to the day and year (see Details).

Usage

aoc_new_day(day, year = NULL)

aoc_new_post(day, year = NULL)

Arguments

day

An integer between 1 and 25

year

An integer representing the year, from 2015 to the current year. Defaults to the current year.

Details

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().

Value

The path to the new day (invisibly)

See Also

aoc_get_input()

Examples

## Not run: aoc_new_day(1, 2022)
## Not run: aoc_new_post(1, 2022)

Create an introduction post

Description

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.

Usage

aoc_new_intro(year = NULL)

Arguments

year

An integer representing the year, from 2015 to the current year. Defaults to the current year.

Value

The path to the introduction post (invisibly)

See Also

aoc_delete_intro() aoc_new_year()

Examples

## Not run: aoc_new_intro(2022)

Create directory and files for a new year

Description

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.

Usage

aoc_new_year(year = NULL, intro = TRUE)

Arguments

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.

Details

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().

Value

The path to the new year directory (invisibly)

See Also

aoc_new_day(), aoc_new_intro(), aoc_delete_intro(), aoc_delete_year()

Examples

## Not run: aoc_new_year(2022)

Get URLs from the Advent of Code website

Description

Get the URL from the Advent of Code website for the puzzle and the puzzle input for a given day and year.

Usage

aoc_url(day, year = NULL, browse = FALSE)

aoc_url_input(day, year = NULL, browse = FALSE)

Arguments

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?

Value

A character string with the URL

Examples

aoc_url(1, 2022)
aoc_url_input(1, 2022)

Extract all numbers from a string

Description

Extract all numbers from a string

Usage

extract_numbers(x)

Arguments

x

character. The string to extract numbers from.

Value

A numeric vector containing all numbers in the string.

Examples

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)

Description

Greatest Common Divisor (GCD) and Least Common Multiple (LCM)

Usage

GCD(x, y)

LCM(x, y)

Arguments

x

A single integer

y

A single integer

Value

The greatest common divisor of x and y

Examples

GCD(12, 18)
GCD(12, 0)
GCD(13, 2)
LCM(12, 18)
LCM(2, 6)
LCM(3, 5)

Convert vectors to a matrix

Description

For the default split of ⁠"⁠, assumes that each element of lines has the same number of characters.

Usage

lines_to_matrix(lines, split = "")

Arguments

lines

a vector

split

character. The string to split the input on. Default is "", i.e. one character per column

Value

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.

Examples

lines_to_matrix(c("#.#.", "..#.", "##.."))

Split input into groups

Description

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().

Usage

split_at_blanks(lines, mode = c("character", "numeric"), split = "")

Arguments

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 "", i.e. split at blank lines

Value

A list, with one element per group originally separated by blank lines.

Examples

x <- c("123", "1234", "", "56", "567", "", "89")
split_at_blanks(x)
split_at_blanks(x, "numeric")