Predict the next value in each sequence, then find their sum.
Toggle the code
# function to take line of input and return a numeric sequencesplit_numeric <-function(seq) {strsplit(seq, " ") |>unlist() |>as.numeric() }# function to predict next valuepredict_oasis <-function(x) {# prediction will be the sum of the final element # of each sequence of differences prediction <-tail(x, 1)while(!all(x ==0)) { x <-diff(x) prediction <- prediction +tail(x, 1) } prediction}input |>lapply(split_numeric) |>sapply(predict_oasis) |>sum()
[1] 1904165718
I was worried when I saw the input about parsing the negative values, but as.numeric() handled that just fine.
Today was the first today that my first submitted answer was incorrect. My ‘gotcha’ turned out to be something else. I was originally using the condition while(sum(x) != 0) for the loop. That works fine on the example, but it turns out in my full input there’s a sequence where the sum is zero before all the individual elements are. Switching the condition to while(!all(x == 0)) fixed the problem.
Part 2
The crux of the puzzle
Extrapolate backwards, figuring out what the first value before the sequence begins is.
This is simply a case of applying predict_oasis to the reversed sequence.
─ Session info ───────────────────────────────────────────────────────────────
setting value
version R version 4.3.2 (2023-10-31)
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-12-09
pandoc 3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
quarto 1.4.523 @ /usr/local/bin/quarto
─ Packages ───────────────────────────────────────────────────────────────────
package * version date (UTC) lib source
aochelpers * 0.1.0.9000 2023-12-06 [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
──────────────────────────────────────────────────────────────────────────────
Source Code
---title: "2023: Day 9"date: 2023-12-9author: - name: Ella Kayecategories: [base R, loops, ⭐⭐]draft: false---## Setup[The original challenge](https://adventofcode.com/2023/day/9)[My data](input){target="_blank"}## Part 1```{r}#| echo: falseOK <-"2023"<3000# Will only evaluate next code block if an actual year has been substituted for the placeholder.``````{r}#| eval: !expr OKlibrary(aochelpers)input <-aoc_input_vector(9, 2023)head(input)```::: {.callout-note collapse="false" icon="false"}## The crux of the puzzlePredict the next value in each sequence, then find their sum.:::```{r}# function to take line of input and return a numeric sequencesplit_numeric <-function(seq) {strsplit(seq, " ") |>unlist() |>as.numeric() }# function to predict next valuepredict_oasis <-function(x) {# prediction will be the sum of the final element # of each sequence of differences prediction <-tail(x, 1)while(!all(x ==0)) { x <-diff(x) prediction <- prediction +tail(x, 1) } prediction}input |>lapply(split_numeric) |>sapply(predict_oasis) |>sum()```I was worried when I saw the input about parsing the negative values, but `as.numeric()` handled that just fine.Today was the first today that my first submitted answer was incorrect.My 'gotcha' turned out to be something else. I was originally using the condition `while(sum(x) != 0)` for the loop.That works fine on the example, but it turns out in my full input there's a sequence where the sum is zero before all the individual elements are. Switching the condition to `while(!all(x == 0))` fixed the problem.## Part 2::: {.callout-note collapse="false" icon="false"}## The crux of the puzzleExtrapolate backwards, figuring out what the first value before the sequence begins is.:::This is simply a case of applying `predict_oasis` to the reversed sequence.```{r}input |>lapply(split_numeric) |>lapply(rev) |>sapply(predict_oasis) |>sum()```##### Session info {.appendix}<details><summary>Toggle</summary>```{r}#| echo: falselibrary(sessioninfo)# save the session info as an objectpkg_session <-session_info(pkgs ="attached")# get the quarto versionquarto_version <-system("quarto --version", intern =TRUE)# inject the quarto infopkg_session$platform$quarto <-paste(system("quarto --version", intern =TRUE), "@", quarto::quarto_path() )# print it outpkg_session```</details>