Exemplo n.º 1
0
        protected override long RunPart2()
        {
            var congruences  = this.ParsedInput.AvailableBusLines.Select(l => l.AsCongruence());
            var intersection = Congruence.RelativelyPrimeIntersection(congruences);

            return(intersection.Offset);
        }
Exemplo n.º 2
0
        private static Congruence RelativelyPrimeIntersection(Congruence one, Congruence other)
        {
            // this only works if this.Modulo and other.Module are relatively prime
            //  since it is trivial to manually check this in the input
            //  I'll skip the rather bothersome step of checking it here

            var modulo = one.Modulo * other.Modulo;
            var offset = one.FirstIntersectingElement(other);

            return(new Congruence(modulo, offset));
        }
Exemplo n.º 3
0
        public long FirstIntersectingElement(Congruence other)
        {
            var value = other.Offset - this.Offset;

            while (true)
            {
                if (value % other.Modulo == 0)
                {
                    return(value - other.Offset);
                }
                else
                {
                    value += this.Modulo;
                }
            }
        }