Toggle the code
library(aochelpers)
<- aoc_input_vector(1, 2022, "numeric")
input head(input, 10)
[1] 5686 2211 1513 7036 5196 10274 2967 2551 NA 5942
Ella Kaye
December 1, 2022
[1] 5686 2211 1513 7036 5196 10274 2967 2551 NA 5942
In this challenge, we’re given groups of numbers and we need to find the sum of each group. Our solution is the largest of these. The groups are separated by a blank line. When reading in the input as a numeric vector, these are coerced to NA
. We can identify the new groups by the NA
values, produce an index for them with cumsum(is.na(input))
, which increments when a new NA
is reached, then use this with split()
to split the input into a list of vectors, one for each group. We need the argument na.rm = TRUE
in sapply()
because each vector, other than the first, starts with NA
, as that’s where it was split.
This is similar, except we want to find the sum of the sums of the top three groups.
─ Session info ───────────────────────────────────────────────────────────────
setting value
version R version 4.3.1 (2023-06-16)
os macOS Sonoma 14.1
system aarch64, darwin20
ui X11
language (EN)
collate en_US.UTF-8
ctype en_US.UTF-8
tz Europe/London
date 2023-11-15
pandoc 3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
quarto 1.4.489 @ /usr/local/bin/quarto
─ Packages ───────────────────────────────────────────────────────────────────
package * version date (UTC) lib source
aochelpers * 0.0.0.9000 2023-11-15 [1] local
sessioninfo * 1.2.2 2021-12-06 [1] CRAN (R 4.3.0)
[1] /Users/ellakaye/Library/R/arm64/4.3/library
[2] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library
──────────────────────────────────────────────────────────────────────────────
---
title: "2022: Day 1"
date: 2022-12-1
author:
- name: Ella Kaye
categories:
- base R
- lists
draft: false
---
## Setup
[The original challenge](https://adventofcode.com/2022/day/1)
[My data](input){target="_blank"}
## Part 1
```{r}
#| echo: false
OK <- "2022" < 3000
# Will only evaluate next code block if an actual year has been substituted for the placeholder
```
```{r}
#| eval: !expr OK
library(aochelpers)
input <- aoc_input_vector(1, 2022, "numeric")
head(input, 10)
```
In this challenge, we're given groups of numbers and we need to find the sum of each group.
Our solution is the largest of these. The groups are separated by a blank line. When reading in the input as a numeric vector, these are coerced to `NA`.
We can identify the new groups by the `NA` values, produce an index for them with `cumsum(is.na(input))`,
which increments when a new `NA` is reached, then use this with `split()` to split the input into a list of vectors, one for each group.
We need the argument `na.rm = TRUE` in `sapply()` because each vector, other than the first, starts with `NA`, as that's where it was split.
```{r}
totals <- split(input, cumsum(is.na(input))) |>
sapply(sum, na.rm = TRUE)
max(totals)
```
## Part 2
This is similar, except we want to find the sum of the sums of the top three groups.
```{r}
sort(totals) |>
tail(3) |>
sum()
```
##### Session info {.appendix}
<details><summary>Toggle</summary>
```{r}
#| echo: false
library(sessioninfo)
# save the session info as an object
pkg_session <- session_info(pkgs = "attached")
# get the quarto version
quarto_version <- system("quarto --version", intern = TRUE)
# inject the quarto info
pkg_session$platform$quarto <- paste(
system("quarto --version", intern = TRUE),
"@",
quarto::quarto_path()
)
# print it out
pkg_session
```
</details>