{"id":19769602,"url":"https://github.com/tjmahr/aoc","last_synced_at":"2025-04-30T17:31:26.324Z","repository":{"id":69780751,"uuid":"433120204","full_name":"tjmahr/aoc","owner":"tjmahr","description":"usethis-like functions for completing Advent of Code as an R package","archived":false,"fork":false,"pushed_at":"2022-12-02T20:28:53.000Z","size":201,"stargazers_count":30,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-26T14:05:33.888Z","etag":null,"topics":["advent-of-code","adventofcode","r"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tjmahr.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-29T16:39:11.000Z","updated_at":"2025-01-13T18:20:42.000Z","dependencies_parsed_at":"2023-04-29T09:02:25.210Z","dependency_job_id":null,"html_url":"https://github.com/tjmahr/aoc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjmahr%2Faoc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjmahr%2Faoc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjmahr%2Faoc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjmahr%2Faoc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tjmahr","download_url":"https://codeload.github.com/tjmahr/aoc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251751163,"owners_count":21637875,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["advent-of-code","adventofcode","r"],"created_at":"2024-11-12T04:44:03.710Z","updated_at":"2025-04-30T17:31:26.305Z","avatar_url":"https://github.com/tjmahr.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\nfile.remove(list.files(\"R\", pattern = \"day.*\", full.names = TRUE))\nfile.remove(list.files(\"inst\", pattern = \"input.*\", full.names = TRUE))\nfile.remove(list.files(\"inst\", pattern = \"run.*\", full.names = TRUE))\nfile.remove(list.files(\"tests/testthat\", pattern = \"test.day*\", full.names = TRUE))\nfile.remove(\"R/data-solutions.R\")\nfile.rename(\".aoccookie\", \".xaoccookie\")\naoc::use_day(01, 2020, open = FALSE)\n```\n\n# aoc \u003cimg src=\"man/figures/logo.png\" align=\"right\" height=\"139\" /\u003e\n\n\u003c!-- badges: start --\u003e\n\u003c!-- badges: end --\u003e\n\naoc provides [usethis](https://usethis.r-lib.org/)-style functions for [Advent\nof Code](https://adventofcode.com) puzzles. This package only downloads content\nfrom the Advent of Code site.\n\n## Installation\n\nYou can install the development version of aoc from [GitHub](https://github.com/) with:\n\n``` r\n# install.packages(\"devtools\")\nremotes::install_github(\"tjmahr/aoc\")\n```\n\n## Preliminaries\n\naoc assumes that we are organizing our R code using an R package. Therefore, it\nrequires a minimal package infrastructure in order to work. In RStudio, using\n\n  - File \\\u003e New Project... \\\u003e New Directory \\\u003e R Package\n  - Setting the package name to `adventofcode21` \\\u003e Create Project\n\nshould create enough of an R package setup for aoc to work.\n\nAlternatively, you can just use `aoc::create_aoc()`, specifying you package \npath as an argument in the function. This will create your aoc package for \nyou and create a dev folder with scripts to help you start working on your \naoc problems.\n\n## Functionality\n\naoc assumes that you are working inside an R package. By default, it assumes the\npackage is named `adventofcodeXX` where XX are the last two digits of the year.\nFor example, `adventofcode20` would be the package for 2020.\n\nFrom this setup, aoc will automate a number of tasks. `use_day()` is a\n[usethis](https://usethis.r-lib.org/)-style function to create placeholder files\nfor each day.\n\n`use_day(day = 1)` does the following tasks:\n\n  - on first run, creates `R/data-solutions.R`\n    - a file for storing solutions\n    \n  - creates `R/day01.R`\n    - puts the puzzle description into a roxygen documentation block\n    - creates placeholder functions for the solutions to day 1\n    \n  - creates `tests/testthat/test-day01.R`\n    - a unit test for day (useful for the example in the puzzle\n      description) in `tests/testthat/test-day01.R`\n      \n  - creates `inst/input01.txt`\n    - a file to hold the input for day 1\n    \n  - creates `inst/run-day01.R` \n    - a file to contain the solution for day 1\n\n```{r, eval = FALSE}\naoc::use_day(1, year = 2020)\n#\u003e ✔ Writing 'R/data-solutions.R'\n#\u003e • Modify 'R/data-solutions.R'\n#\u003e downloading puzzle html using .aoccookie\n#\u003e Executing: pandoc -t markdown -o \n#\u003e \"C:\\Users\\trist\\AppData\\Local\\Temp\\RtmpK8sv2r\\file228c54056ded.markdown\" \n#\u003e \"C:\\Users\\trist\\AppData\\Local\\Temp\\RtmpK8sv2r\\file228c54056ded.html\"\n#\u003e ✔ Writing 'R/day01.R'\n#\u003e ● Write your solution code here\n#\u003e ● Once you unlock Part Two, update the Roxygen block with the description\n#\u003e ✔ Writing 'inst/input01.txt'\n#\u003e ● Copy your problem input into this file\n#\u003e ✔ Writing 'tests/testthat/test-day01.R'\n#\u003e • Edit 'tests/testthat/test-day01.R'\n#\u003e ● Write unit tests using the examples from the problem description\n#\u003e ✔ Writing 'inst/run-day01.R'\n#\u003e ● Run your solution on the input here. Once it works, update R/data-solutions.R\n```\n\n`R/data-solutions.R` is where we store our solutions:\n\n```{r, echo = FALSE}\npreview_lines \u003c- function(path, lines = 10) {\n  # path \u003c- \"R/utils-usethis.R\"\n  l \u003c- readLines(path)\n  if (lines * 2 \u003c length(l)) {\n    show \u003c- c(head(l, lines), \"[... truncated ...]\")\n  } else {\n    show \u003c- l\n  }\n  writeLines(show)\n}\n```\n\n\n```{r, echo = FALSE, comment = \"\"}\npreview_lines(\"R/data-solutions.R\", 20)\n```\n\n`inst/input01.txt` is an empty file for our input data. We have to paste in our\npuzzle input here.\n\nThe R script for `R/day01.R` provides the puzzle description for part 1,\nfunction stubs for part 1 `f01a()` and part 2 `f01b()`. I also like to make the\nexample data into a function for unit tests or code examples, so there is a stub\nfor `example_data_01()`.\n\n```{r, echo = FALSE, comment = \"\"}\npreview_lines(\"R/day01.R\", 200)\n```\n\n`tests/testthat/test-day01.R` is a placeholder for file unit tests. It's a good\nplace work through the examples in the puzzle description.\n\n```{r, echo = FALSE, comment = \"\"}\npreview_lines(\"tests/testthat/test-day01.R\")\n```\n\nOnce we have developed a solution for the example input, we can test our\nofficial input by running the code in `inst/run-day01.R`. The final two lines\nprovide code to validate the solutions that we store in `R/data-solutions.R`.\n\n```{r, echo = FALSE, comment = \"\"}\npreview_lines(\"inst/run-day01.R\")\n```\n\n\n\n### Default values for `use_day()`\n\nWe can tell aoc which year to use by using `options()`.\n\n```{r, eval = TRUE}\noptions(aoc.year = 2017)\naoc::use_day(3)\n```\n\nIf we look at the first lines of `R/day03.R`, we can see the correct URL used.\n\n```{r, echo = FALSE, comment = \"\"}\npreview_lines(\"R/day03.R\")\n```\n\nWe can also tell aoc which package name to use for our project using\n`options()`.\n\n```{r, eval = TRUE}\noptions(aoc.package = \"awesomeadvent2017\")\naoc::use_day(4)\n```\n\nAnd here the correct name appears in the `library()` call.\n\n```{r, echo = FALSE, comment = \"\"}\npreview_lines(\"inst/run-day04.R\")\n```\n\nWe can set these permanently for an Advent of Code package by editing the\npackage's `.Rprofile`:\n\n```{r, eval = FALSE}\nusethis::edit_r_profile(scope = \"project\")\n# add in things like `options(aoc.year = 2017)`\n```\n\n### Advanced: Using a user cookie \n\nIf you know how to retrieve the cookie for your Advent of Code user, you can use\nthis cookie to download your puzzle input. Store the cookie in file named\n`.aoccookie`. Then `use_day()` will automatically use this cookie when\ndownloading puzzle input.\n\nFor these demos, I hid my cookie by renaming the file. If I unrename the file\nand download the day 7 files, I can preview the lines of the input file.\n\n```{r}\nfile.rename(\".xaoccookie\", \".aoccookie\")\n\naoc::use_day(7)\n\n# this is a function i defined in a hidden code block 🤫\npreview_lines(\"inst/input07.txt\")\n```\n\nThe other advantage of a user cookie is that after solving part 1 of a day, we\ncan download part 2 as a roxygen2 block. By default, this block is copied to the \nclipboard, but for this demo, I have to disable it.\n\n```{r, comment = \"\"}\naoc::download_part2_to_roxygen_md(day = 7, clip = FALSE)\n```\n\n\n```{r, include = FALSE}\nfile.remove(list.files(\"R\", pattern = \"day.*\", full.names = TRUE))\nfile.remove(list.files(\"inst\", pattern = \"input.*\", full.names = TRUE))\nfile.remove(list.files(\"inst\", pattern = \"run.*\", full.names = TRUE))\nfile.remove(list.files(\"tests/testthat\", pattern = \"test.day*\", full.names = TRUE))\nfile.remove(\"R/data-solutions.R\")\n```\n\n## Credit\n\nThanks to [{golem}](https://github.com/ThinkR-open/golem) for their implementation of dev files to support \npackage development. `aoc::create_aoc()` is inspired by and based on `golem::create_golem()`.\n\nHex icon created using the [hexmake\napp](https://connect.thinkr.fr/hexmake/) from\n[ColinFay](https://github.com/ColinFay/hexmake).\n\n\u003ca href=\"https://www.flaticon.com/free-icons/star\" title=\"star icons\"\u003eStar icons created by Freepik - Flaticon\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftjmahr%2Faoc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftjmahr%2Faoc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftjmahr%2Faoc/lists"}