2023 - day 09

Author

Hendrik Wagenseil

head(txt)
[1] "13 9 1 2 50 233 728 1871 4300 9258 19218 39107 78571 155948 304911 585118 1098671 2014751 3605471 6296784 10739208"       
[2] "16 39 84 173 354 719 1433 2795 5379 10350 20134 39782 79679 160823 324893 652941 1299026 2548741 4917680 9311783 17280520"
[3] "0 14 39 74 117 165 221 322 616 1545 4242 11327 28389 66554 146616 305124 602288 1130037 2015139 3405883 5420809"          
[4] "6 14 26 55 130 298 626 1203 2142 3582 5690 8663 12730 18154 25234 34307 45750 59982 77466 98711 124274"                   
[5] "15 27 57 112 193 306 501 952 2094 4836 10868 23071 46018 86517 154090 261201 422937 655705 974331 1386730 1885055"        
[6] "4 12 28 52 84 124 172 228 292 364 444 532 628 732 844 964 1092 1228 1372 1524 1684"                                       

Continue series of numbers by differencing. Add one more element and submit sum of these extrapolated values.

lapply(
  txt
  , FUN = \(i){
    # i = txt[1]
    l = strsplit(i, split = " ")[[1]] |> 
      as.integer() |> 
      list()
    
    while (sum(abs(l[[1]])) > 0L){
      l = c(list(diff(l[[1]])), l)
    }

    incr = integer(length(l))
    incr[1] = 0
    
    for (k in seq_along(l)){
      l[[k]] = c(l[[k]], l[[k]][length(l[[k]])] + incr[k])
      incr[k + 1] = l[[k]][length(l[[k]])]
    }
    
    l[[length(l)]][length(l[[length(l)]])]
     
  }
  
) |> 
  unlist() |> 
  sum()
[1] 1987402313

Add \(m\) successive cards where \(m\) is the number of matches in a card. Get the final number of cards.