{"id":14067081,"url":"https://github.com/dgrtwo/adventdrob","last_synced_at":"2025-10-06T16:56:02.253Z","repository":{"id":149050915,"uuid":"437380068","full_name":"dgrtwo/adventdrob","owner":"dgrtwo","description":"Personal R package for Advent of Code","archived":false,"fork":false,"pushed_at":"2022-12-12T05:50:18.000Z","size":18,"stargazers_count":20,"open_issues_count":0,"forks_count":1,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-02-28T06:55:19.698Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dgrtwo.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-12-11T20:17:36.000Z","updated_at":"2024-08-14T04:04:57.000Z","dependencies_parsed_at":null,"dependency_job_id":"104a9c63-a973-4aa1-b155-d738be5f010e","html_url":"https://github.com/dgrtwo/adventdrob","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/dgrtwo%2Fadventdrob","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrtwo%2Fadventdrob/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrtwo%2Fadventdrob/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrtwo%2Fadventdrob/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dgrtwo","download_url":"https://codeload.github.com/dgrtwo/adventdrob/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243902240,"owners_count":20366258,"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":[],"created_at":"2024-08-13T07:05:25.443Z","updated_at":"2025-10-06T16:55:57.232Z","avatar_url":"https://github.com/dgrtwo.png","language":"R","funding_links":[],"categories":["R"],"sub_categories":[],"readme":"\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)\n```\n\n# DRob's Advent of Code functions\n\n\u003c!-- badges: start --\u003e\n\u003c!-- badges: end --\u003e\n\nPersonal R package meant to help me compete in [Advent of Code](https://adventofcode.com/), especially functions for working with data formats common in the puzzles such as grids. This includes:\n\n* An `advent_input` function that reads in a one-column tibble\n* Functions for parsing grids (helpful on days 3, 4, 9, and 11 of 2021)\n\nFeel free to use it yourself! Note I don't intend to maintain or support it, but I might add to it as 2021's Advent goes along and I find other patterns that pop up multiple times.\n\n## Installation\n\nYou can install the development version of adventdrob like so:\n\n``` r\ndevtools::install_github(\"dgrtwo/adventdrob\")\n```\n\nYou'll then have to set ADVENT_SESSION in your .Renviron to your Advent of Code cookie. Example of how to get that in Chrome:\n\n* Visit [adventofcode.com](https://adventofcode.com/), and log in\n* Right click + Inspect to view Developer Tools\n* Select the Network tab\n* Refresh the page, and select the \"adventofcode.com\" request\n* Under Request Headers, there should be a cookie including `session=\u003ccookie here\u003e`. Copy that without the session=.\n\n## Example: 2021 Day 9\n\n[Day 9 of the 2021 puzzle](https://adventofcode.com/2021/day/9) provides a grid of digits. `advent_input()` reads this into a one-column tibble.\n\n```{r example}\nlibrary(dplyr)\nlibrary(adventdrob)\n\ninput \u003c- advent_input(9, 2021)\ninput\n```\n\n### Part 1\n\nPart 1 tasks us to find the points that are lower than any adjacent point. Excerpts from the puzzle follow.\n\n\u003e Your first goal is to find the low points - the locations that are lower than any of its adjacent locations... (Diagonal locations do not count as adjacent.)\n\n\u003e The risk level of a low point is 1 plus its height.\n\n\u003e Find all of the low points on your heightmap. What is the sum of the risk levels of all low points on your heightmap?\n\nThe input can be parsed with `grid_tidy` to create a tidy one-row-per-cell form of row, column and value.\n\n```{r}\ninput %\u003e%\n  grid_tidy(x)\n```\n\nWe can use `adjacent_join` to join each `row/col` pair with the adjacent `row2/col2` pair.\n\n```{r}\ninput %\u003e%\n  grid_tidy(x) %\u003e%\n  adjacent_join()\n```\n\nWith a bit of summarizing, we can find all points that are greater than all their neighbors.\n\n```{r}\ninput %\u003e%\n  grid_tidy(x) %\u003e%\n  adjacent_join() %\u003e%\n  group_by(row, col, value) %\u003e%\n  summarize(low = all(value2 \u003e value), .groups = \"drop\") %\u003e%\n  filter(low) %\u003e%\n  summarize(sum(value + 1))\n```\n\n### Part 2\n\nFor some problems, the best form isn't a tidy table, but a [tidygraph](https://github.com/thomasp85/tidygraph) object.\n\n\u003e A basin is all locations that eventually flow downward to a single low point. Therefore, every low point has a basin, although some basins are very small. Locations of height 9 do not count as being in any basin, and all other locations will always be part of exactly one basin.\n\n\u003e The size of a basin is the number of locations within the basin, including the low point.\n\n\u003e What do you get if you multiply together the sizes of the three largest basins?\n\nThis is well suited to finding connected components within a graph. (Many thanks to Jarosław Nirski on the [R for Data Science Slack](https://rfordatascience.slack.com/) for this approach!)\n\n```{r}\ninput %\u003e%\n  grid_graph(x)\n```\n\nAs it happens, this lets us answer part 2 in a few lines, thanks to the `group_components()` function in tidygraph.\n\n```{r}\nadvent_input(9) %\u003e%\n  grid_graph(x) %\u003e%\n  filter(value != 9) %\u003e%\n  mutate(component = tidygraph::group_components()) %\u003e%\n  as_tibble() %\u003e%\n  count(component, sort = TRUE) %\u003e%\n  head(3) %\u003e%\n  summarize(answer = prod(n))\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgrtwo%2Fadventdrob","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdgrtwo%2Fadventdrob","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgrtwo%2Fadventdrob/lists"}