01020304050607080910111213141516171819202122232425

Advent of Code

2017/17

Spinlock

in C#

by encse

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.

Read the full puzzle.

using System.Collections.Generic;

namespace AdventOfCode.Y2017.Day17;

[ProblemName("Spinlock")]
class Solution : Solver {

    public object PartOne(string input) {
        var step = int.Parse(input);
        var nums = new List<int>() { 0 };
        var pos = 0;
        for (int i = 1; i < 2018; i++) {
            pos = (pos + step) % nums.Count + 1;
            nums.Insert(pos, i);
        }
        return nums[(pos + 1) % nums.Count];
    }

    public object PartTwo(string input) {
        var step = int.Parse(input);
        var pos = 0;
        var numsCount = 1;
        var res = 0;
        for (int i = 1; i < 50000001; i++) {
            pos = (pos + step) % numsCount + 1;
            if (pos == 1) {
                res = i;
            }
            numsCount++;
        }
        return res;
    }
}

Please ☆ my repo if you like it!

© 2025 Advent of Code is a registered trademark in the US Images provided by Bing image creator