예제 #1
0
        public static void Main()
        {
            string source = File.ReadAllText(@"..\..\input.txt");

            source = source.Remove(source.Length - 1);
            List <string> instructions = source.Split('\n').ToList();

            List <Reindeer> reindeers = instructions.Select(i => new Reindeer(i)).ToList();

            const int secondsCount            = 2503;
            var       winningReindeerDistance = GetWinningReindeerDistance(reindeers, secondsCount);

            List <RaceState> racers = reindeers.Select(r => new RaceState {
                Reindeer = r
            }).ToList();
            RaceState winnerOfSecondRace = GetWinnerOfSecondRace(secondsCount, racers);

            Console.WriteLine($"Winning distance of first race: {winningReindeerDistance}");
            Console.WriteLine($"Winner of second race: {winnerOfSecondRace.Reindeer.Name} with {winnerOfSecondRace.Points} points.");
            Console.ReadKey();
        }
예제 #2
0
        private static RaceState GetWinnerOfSecondRace(int secondsCount, List <RaceState> racers)
        {
            for (int i = 0; i < secondsCount; i++)
            {
                foreach (RaceState racer in racers)
                {
                    if (racer.RunTime == racer.Reindeer.TravelTime)
                    {
                        racer.RunTime  = 0;
                        racer.RestTime = racer.Reindeer.RestTime - 1;
                        continue;
                    }

                    if (racer.RestTime > 0)
                    {
                        racer.RestTime -= 1;
                        continue;
                    }

                    racer.Traveled += racer.Reindeer.Velocity;
                    racer.RunTime  += 1;
                }

                int currentWinningDistance = racers.Select(r => r.Traveled).Max();

                foreach (RaceState racer in racers)
                {
                    if (racer.Traveled == currentWinningDistance)
                    {
                        racer.Points += 1;
                    }
                }
            }

            RaceState winnerOfSecondRace = racers.OrderByDescending(r => r.Points).First();

            return(winnerOfSecondRace);
        }