2017-12-17 - Spinlock
(original .ipynb)test_skip = 3 puzzle_skip = 344 def part_one(skip): buffer = [0] val = 1 pos = 0 while val <= 2017: pos = (pos + skip) % val + 1 buffer.insert(pos, val) val += 1 return buffer[pos+1] assert 638 == part_one(test_skip) print("part one:", part_one(puzzle_skip))
part one: 996
# realised we actually don't need to build the buffer, just track what we # _would_ have written to position [1] def part_two(): val = 1 pos = 0 after_zero = None while val <= 50000000: pos = (pos + puzzle_skip) % val + 1 if pos == 1: after_zero = val val += 1 return after_zero print("part two:", part_two())
part two: 1898341