public void Solve()
            {
                string nextLine;
                var    regexPattern =
                    Stex.Begin +
                    "Disc #" +
                    Stex.Integer("Index") +
                    " has " +
                    Stex.Integer("Positions") +
                    " positions; at time=0, it is at position " +
                    Stex.Integer("Start") +
                    "." +
                    Stex.End;
                var rgx = new Regex(regexPattern);

                var a    = new List <long>();
                var mods = new List <long>();

                while ((nextLine = ReadLine()) != null)
                {
                    var match = rgx.Match(nextLine);
                    var index = int.Parse(match.Groups["Index"].Value);
                    var mod   = int.Parse(match.Groups["Positions"].Value);
                    var start = int.Parse(match.Groups["Start"].Value);
                    a.Add((100 * mod - start - index) % mod);
                    mods.Add(mod);
                }
                WriteLine(ChineseRemainder.CRT(a.ToArray(), mods.ToArray()));
                a.Add(11 - a.Count - 1);
                mods.Add(11);
                WriteLine(ChineseRemainder.CRT(a.ToArray(), mods.ToArray()));
            }
Exemplo n.º 2
0
            public void Solve()
            {
                _startTime = GetVal();
                _busIds    = ReadLine().
                             Split(',', int.MaxValue).
                             Select(s => s[0] == 'x' ? -1 : int.Parse(s)).
                             ToArray();

                // Part 1
                var waitTime = int.MaxValue;
                var bestBus  = -1;

                foreach (var busId in _busIds.Where(id => id > 0))
                {
                    var rem = _startTime % busId;
                    if (rem == 0)
                    {
                        waitTime = 0;
                        bestBus  = busId;
                    }

                    var thisWait = busId - rem;
                    if (thisWait < waitTime)
                    {
                        waitTime = thisWait;
                        bestBus  = busId;
                    }
                }

                WriteLine(waitTime * bestBus);

                // Part 2
                // Chinese Remainder Theorem

                var mods  = _busIds.Where(v => v > 0).Select(v => (long)v).ToArray();
                var aVals = new long[mods.Length];
                var iVals = 0;

                for (var i = 0; i < _busIds.Length; i++)
                {
                    if (_busIds[i] > 0)
                    {
                        aVals[iVals++] = _busIds[i] - i;
                    }
                }

                var soln = ChineseRemainder.CRT(aVals, mods);

                WriteLine(soln);
            }