Brief
Suddenly, whirling in the distance, you notice what looks like a massive, pixelated hurricane: a deadly spinlock. This spinlock isn’t just consuming computing power, but memory, too; vast, digital mountains are being ripped from the ground and consumed by the vortex.
If you don’t move quickly, fixing that printer will be the least of your problems.
This spinlock’s algorithm is simple but efficient, quickly consuming everything in its path. It starts with a circular buffer containing only the value 0, which it marks as the current position. It then steps forward through the circular buffer some number of steps (your puzzle input) before inserting the first new value, 1, after the value it stopped on. The inserted value becomes the current position. Then, it steps forward from there the same number of steps, and wherever it stops, inserts after it the second new value, 2, and uses that as the new current position again.
It repeats this process of stepping forward, inserting a new value, and using the location of the inserted value as the new current position a total of 2017 times, inserting 2017 as its final operation, and ending with a total of 2018 values (including 0) in the circular buffer.
For example, if the spinlock were to step 3 times per insert, the circular buffer would begin to evolve like this (using parentheses to mark the current position after each iteration of the algorithm):
(0)
, the initial state before any insertions. 0 (1):
the spinlock steps forward three times (0, 0, 0), and then inserts the first value, 1, after it. 1 becomes the current position. 0 (2) 1:
the spinlock steps forward three times (0, 1, 0), and then inserts the second value, 2, after it. 2 becomes the current position. 0 2 (3) 1:
the spinlock steps forward three times (1, 0, 2), and then inserts the third value, 3, after it. 3 becomes the current position. And so on:
0 2 (4) 3 1
0 (5) 2 4 3 1
0 5 2 4 3 (6) 1
0 5 (7) 2 4 3 6 1
0 5 7 2 4 3 (8) 6 1
0 (9) 5 7 2 4 3 8 6 1
Eventually, after 2017 insertions, the section of the circular buffer near the last insertion looks like this:
1512 1134 151 (2017) 638 1513 851
Perhaps, if you can identify the value that will ultimately be after the last value written (2017), you can short-circuit the spinlock. In this example, that would be 638.
What is the value after 2017 in your completed circular buffer?
Let’s go
Packages & functions
library(tidyverse)
[37m── [1mAttaching packages[22m ───────────────────────────────────────────────────────── tidyverse 1.2.1 ──[39m
[37m[32m✔[37m [34mggplot2[37m 2.2.1.[31m9000[37m [32m✔[37m [34mpurrr [37m 0.2.4
[32m✔[37m [34mtibble [37m 1.3.4 [32m✔[37m [34mdplyr [37m 0.7.4
[32m✔[37m [34mtidyr [37m 0.7.2 [32m✔[37m [34mstringr[37m 1.2.0
[32m✔[37m [34mreadr [37m 1.1.1 [32m✔[37m [34mforcats[37m 0.2.0 [39m
[37m── [1mConflicts[22m ──────────────────────────────────────────────────────────── tidyverse_conflicts() ──
[31m✖[37m [34mdplyr[37m::[32mfilter()[37m masks [34mstats[37m::filter()
[31m✖[37m [34mdplyr[37m::[32mlag()[37m masks [34mstats[37m::lag()[39m
library(testthat)
Attaching package: ‘testthat’
The following object is masked from ‘package:dplyr’:
matches
The following object is masked from ‘package:purrr’:
is_null
library(aocodeR)
library(hash)
hash-2.2.6 provided by Decision Patterns
Functions
spin_lock <- function(end = 2017, steps = input) {
cp <- 1
circ_buf <- 0
for(i in 1:end){
np <- (cp - 1 + steps) %% length(circ_buf) + 1
circ_buf <- append(circ_buf, i, np)
cp <- np + 1
}
list(cp = cp, circ_buf = circ_buf)
}
next_value <- function(x) {
x[[2]][x[[1]] + 1]
}
end <- 5000
t0 <- Sys.time()
t <-spin_lock(end, input)
(Sys.time() - t0) * 50000000/end
Time difference of 1968.579 secs
Time taken per iteration increases with the size of end
(ie the size of the final vector)
Test
expect_equal(spin_lock(9, 3),
list(cp = 2,
circ_buf = c(0, 9, 5, 7, 2, 4, 3, 8, 6, 1))
)
deploy
`
spin_lock(2017, input) %>% next_value
[1] 1914
Success!
LS0tCnRpdGxlOiAiLS0tIERheSAxNzogU3BpbmxvY2sgLS0tIgphdXRob3I6ICJhbm5ha3J5c3RhbGxpIgpkYXRlOiAyMDE3LTEyLTE3Cm91dHB1dDogaHRtbF9ub3RlYm9vawplZGl0b3Jfb3B0aW9uczogCiAgY2h1bmtfb3V0cHV0X3R5cGU6IGlubGluZQotLS0KCmBgYHtyIGtuaXRyLW9wdHMtY2h1bmssIGluY2x1ZGU9RkFMU0V9CiMgVXBkYXRlIGtuaXRyIGNodW5rIG9wdGlvbnMKIyBodHRwczovL3lpaHVpLm5hbWUva25pdHIvb3B0aW9uc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