예제 #1
0
        public override long SolvePart1()
        {
            foreach (string busId in busIds)
            {
                if (busId.Equals("x"))
                {
                    continue;
                }

                BusTimestamp busTimestamp     = null;
                long         busIdConverted   = Convert.ToInt32(busId);
                long         currentTimestamp = busIdConverted;

                while (busTimestamp == null)
                {
                    if (currentTimestamp >= earliestTimestamp)
                    {
                        busTimestamp = new BusTimestamp(busIdConverted, currentTimestamp);
                    }

                    currentTimestamp += busIdConverted;
                }

                if (!availableTimestamps.ContainsKey(busTimestamp.Timestamp))
                {
                    availableTimestamps.Add(busTimestamp.Timestamp, busTimestamp.BusId);
                }
            }

            BusTimestamp earliestAvailableTimestamp = GetEarliestAvailableTimestamp();

            return(earliestAvailableTimestamp.BusId * (earliestAvailableTimestamp.Timestamp - earliestTimestamp));
        }
예제 #2
0
        private BusTimestamp GetEarliestAvailableTimestamp()
        {
            BusTimestamp earliestAvailableTimestamp = null;
            long         currentTimestamp           = earliestTimestamp;

            while (earliestAvailableTimestamp == null)
            {
                if (availableTimestamps.ContainsKey(currentTimestamp))
                {
                    earliestAvailableTimestamp = new BusTimestamp((long)availableTimestamps[currentTimestamp], currentTimestamp);
                }

                currentTimestamp++;
            }

            return(earliestAvailableTimestamp);
        }