Day 17

We're asked about this 'spinlock' thing which I gather to be a way of doing some sort of repetitive process in order to stall an intruder. The idea is that we have an increment and a fixed number (in our case 304), where we move forward along a ring of numbers that many paces. Then we stop, add in the increment, and increase it by one. We're asked what the value following 2017 will be if we run it that long. Doesn't seem too hard...

In [1]:
import pandas as pd
import numpy as np

lock_num = 304
spinlock = [0]
loc = 0

for j in range(1, 2018):
    loc = (loc + lock_num) % len(spinlock)
    spinlock.insert(loc + 1, j)
    loc = (loc + 1) % len(spinlock)
    #print('Iteration', j, 'gives', spin_loc, 'and position', loc)

spinlock_arr = np.array(spinlock)
where2017 = np.where(spinlock_arr == 2017)
spinlock[where2017[0][0] + 1]
Out[1]:
1173

Part 2

It seems like the difficulty of the first parts of these puzzles is pretty monotone.

This one seems like it's another one where we're supposed to be tricky and not just have the computer iterate forever. We're asked what comes after 0 when the 50,000,000th value is inserted...

So my previous approach is very slow to make the actual list but maybe we can make it run if we think of it in a specific way. We'll hold the ring such that zero is always the first element of the array. Then we'll only do something if the location ends up being the second element. In this case we'll update:

In [2]:
lock_num = 304
loc = 0
second = None

for j in range(1, 50000001):
    check = (loc + lock_num) % j
    if check == 0:
        second = j
    loc = check + 1
second
Out[2]:
1930815