01020304050607080910111213141516171819202122232425

Advent of Code

2016/15

Timing is Everything

in C#

by encse

The halls open into an interior plaza containing a large kinetic sculpture. The sculpture is in a sealed enclosure and seems to involve a set of identical spherical capsules that are carried to the top and allowed to bounce through the maze of spinning pieces.

Part of the sculpture is even interactive! When a button is pressed, a capsule is dropped and tries to fall through slots in a set of rotating discs to finally go through a little hole at the bottom and come out of the sculpture. If any of the slots aren't aligned with the capsule as it passes, the capsule bounces off the disc and soars away. You feel compelled to get one of those capsules.

Read the full puzzle.

using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace AdventOfCode.Y2016.Day15;

[ProblemName("Timing is Everything")]
class Solution : Solver {

    public object PartOne(string input) => Iterate(Parse(input)).First(v => v.ok).t;

    public object PartTwo(string input) => Iterate(Parse(input).Concat(new []{(pos: 0, mod: 11)}).ToArray()).First(v => v.ok).t;

    (int pos, int mod)[] Parse(string input) => (
            from line in input.Split('\n')
            let m = Regex.Match(line, @"Disc #\d has (\d+) positions; at time=0, it is at position (\d+).")
            select (pos: int.Parse(m.Groups[2].Value), mod: int.Parse(m.Groups[1].Value))
        ).ToArray();

    IEnumerable<(int t, bool ok)> Iterate((int pos, int mod)[] discs) {
        for (int t = 0; ; t++) {
            var ok = Enumerable.Range(0, discs.Length)
                .All(i => (discs[i].pos + t + i + 1) % discs[i].mod == 0);
            yield return (t, ok);
        }
    }
}

Please ☆ my repo if you like it!

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