base R
loops
⭐⭐
Author

Ella Kaye

Published

December 9, 2023

Setup

The original challenge

My data

Part 1

Toggle the code
library(aochelpers)
input <- aoc_input_vector(9, 2023)
head(input)
[1] "1 2 5 13 33 89 245 643 1565 3535 7495 15128 29479 56181 105913 199391 377649 723582 1407901 2788593 5627669"           
[2] "18 37 79 153 277 493 883 1592 2876 5211 9525 17649 33125 62564 117833 219509 402350 724133 1280307 2228813 3832565"    
[3] "0 -9 -18 -18 16 148 529 1485 3668 8317 17736 36227 71969 140778 273424 529425 1022448 1966692 3762308 7156179 13552786"
[4] "-6 -13 -17 3 93 334 845 1783 3355 5876 9945 16889 29783 55659 110077 226237 472643 986772 2038810 4152178 8331240"     
[5] "12 16 19 16 -4 -57 -159 -311 -455 -360 650 4330 14992 42932 112235 278107 664353 1542439 3494604 7743519 16805383"     
[6] "5 12 26 67 161 344 682 1315 2533 4892 9378 17627 32209 56984 97538 161707 260197 407308 621770 927699 1355681"         
The crux of the puzzle

Predict the next value in each sequence, then find their sum.

Toggle the code
# function to take line of input and return a numeric sequence
split_numeric <- function(seq) {
  strsplit(seq, " ") |> unlist() |> as.numeric() 
}

# function to predict next value
predict_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.

Toggle the code
input |> 
  lapply(split_numeric) |> 
  lapply(rev) |> 
  sapply(predict_oasis) |> 
  sum()
[1] 964

Session info

Toggle
─ 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

──────────────────────────────────────────────────────────────────────────────