public int Resolve(TrainSimulator simulator)
        {
            _trainSimulator = simulator;

            var targetCarriageState = _trainSimulator.CurrentCarriage.LightIsOn;
            var iterationsCount     = 0;

            while (true)
            {
                do
                {
                    _trainSimulator.GoNextCarriage();
                    iterationsCount++;
                } while (targetCarriageState != _trainSimulator.CurrentCarriage.LightIsOn);

                Iterate(iterationsCount, () => _trainSimulator.GoPreviousCarriage());

                _trainSimulator.CurrentCarriage.ToggleLight();
                targetCarriageState = !targetCarriageState;

                Iterate(iterationsCount, () => _trainSimulator.GoNextCarriage());


                if (_trainSimulator.CurrentCarriage.LightIsOn == targetCarriageState)
                {
                    break;
                }
            }

            return(iterationsCount);
        }
示例#2
0
        static void Main(string[] args)
        {
            var simulator = new TrainSimulator(CarriageCount);

            var result = new TrainTaskResolver().Resolve(simulator);

            Console.WriteLine($"{result}");
            Console.ReadKey();
        }