day 17
spinlock
approach
part 1 builds the circular buffer for real with a hand-written doubly linked Node class: it steps forward steps % i nodes, inserts the new value after the current node, and moves onto it. after 2017 insertions the answer is the value after the last one inserted.
part 2 goes to 50 million insertions, too many for a real buffer. value 0 never moves from the front, so the code only tracks the current position and the buffer size, and remembers the last value that would have been inserted at position 1, right after 0.
the trick
you only care about the slot after 0, so you can track positions and never build the buffer.
python
def part2(steps):
cur = 0
size = 1
ntzero = None
for i in range(1, 50000001):
cur = ((cur + steps) % size) + 1
if cur == 1:
ntzero = i
size += 1
return ntzero