Skip to main content

aoc/year2017/
day17.rs

1//! # Spinlock
2//!
3//! For part one, we simulate 2017 rounds and record the position (index) at which each number
4//! is inserted. We then find the index after the number 2017. Finally, we iterate backwards
5//! through the stored indexes to find the first (i.e. last) number inserted at that index.
6//!
7//! There are two insights that speed up part two.
8//!
9//! The first is that we don't need a buffer. We only need to preserve the last value inserted
10//! whenever the index becomes zero. Once 50 million values have been inserted then this value
11//! is the final result.
12//!
13//! The second trick is realizing that we can insert multiple values at a time before the index
14//! wraps around. For example, if the index is 1, the current value 10,000 and the step 300,
15//! then we can insert 34 values at once. The [`div_ceil`] method is perfect for this computation.
16//!
17//! The number of skips grows geometrically, or `O(ln n)` overall effort, learning the answer in
18//! just a few thousand iterations.
19//!
20//! [`div_ceil`]: usize::div_ceil
21use crate::util::parse::*;
22
23type Input = (usize, usize);
24
25pub fn parse(input: &str) -> Input {
26    let step = input.unsigned::<usize>() + 1;
27
28    // For part one, track the index every node had when inserted.
29    let mut index = 0;
30    let indices = Vec::from_fn(2017, |i| {
31        index = (index + step) % (i + 1);
32        index
33    });
34
35    // Now back up to find the prior node that shares the same index, accounting for when
36    // the index moved because an intermediate number was assigned an earlier index.
37    let mut next = (index + 1) % 2017;
38    let mut part_one = 0;
39
40    for (i, &o) in indices.iter().enumerate().rev() {
41        if o == next {
42            part_one = i + 1;
43            break;
44        }
45        if o < next {
46            next -= 1;
47        }
48    }
49
50    // For part two, we only need to focus on nodes inserted at index 0.
51    let mut n = 2017;
52    let mut part_two = 0;
53
54    while n <= 50_000_000 {
55        if index == 0 {
56            part_two = n;
57        }
58
59        let skip = (n - index).div_ceil(step - 1);
60        n += skip;
61        // Here, n is larger than 2017, while step is on the order of 300 to 400. Therefore, step
62        // wraps only once, and subtraction works instead of modulus.
63        index = index + skip * step - n;
64    }
65
66    (part_one, part_two)
67}
68
69pub fn part1(input: &Input) -> usize {
70    input.0
71}
72
73pub fn part2(input: &Input) -> usize {
74    input.1
75}