コード例 #1
0
ファイル: TestCase.cs プロジェクト: UIILabsDev/CodeJam
        public object Solve()
        {
            var dependencies = new DependencyMatrix(cars);
            var lanes = new LaneList(cars);

            var initialListIsOk = lanes.TryUpdate(dependencies);
            Debug.Assert(initialListIsOk);

            foreach (var e in Events.Enumerate(cars))
            {
                Debug.Assert(e.Time >= 0);

                if (e.Type == EventType.StartOverlapping)
                {
                    if (!dependencies.TryPutCarsOnOppositeLanes(e.Car1, e.Car2))
                        return e.Time;
                }

                if (e.Type == EventType.StopOverlapping)
                {
                    dependencies.RemoveCarsOnOppositeLanes(e.Car1, e.Car2);
                }

                if (!lanes.TryUpdate(dependencies))
                    return e.Time;
            }

            return "Possible";
        }
コード例 #2
0
ファイル: LaneList.cs プロジェクト: UIILabsDev/CodeJam
        public bool TryUpdate(DependencyMatrix dependencies)
        {
            for (int car1 = 0; car1 < lanes.Length; car1++)
            {
                if (dependencies.IsIndependent(car1))
                {
                    lanes[car1] = Lane.Undeterminated;
                    continue;
                }

                foreach (var car2 in dependencies.GetCarsOnSameLane(car1))
                {
                    if (lanes[car2] == Lane.Undeterminated) continue;

                    if (lanes[car1] == Lane.Undeterminated)
                        lanes[car1] = lanes[car2];
                    else if (lanes[car1] != lanes[car2])
                        return false;
                }

                foreach (var car2 in dependencies.GetCarsOnOppositeLane(car1))
                {
                    if (lanes[car2] == Lane.Undeterminated) continue;

                    if (lanes[car1] == Lane.Undeterminated)
                        lanes[car1] = lanes[car2].Opposite();
                    else if (lanes[car1] == lanes[car2])
                        return false;
                }
            }

            return true;
        }
コード例 #3
0
ファイル: TestCase.cs プロジェクト: yash1998/CodeJam
        public object Solve()
        {
            var dependencies = new DependencyMatrix(cars);
            var lanes        = new LaneList(cars);

            var initialListIsOk = lanes.TryUpdate(dependencies);

            Debug.Assert(initialListIsOk);

            foreach (var e in Events.Enumerate(cars))
            {
                Debug.Assert(e.Time >= 0);

                if (e.Type == EventType.StartOverlapping)
                {
                    if (!dependencies.TryPutCarsOnOppositeLanes(e.Car1, e.Car2))
                    {
                        return(e.Time);
                    }
                }

                if (e.Type == EventType.StopOverlapping)
                {
                    dependencies.RemoveCarsOnOppositeLanes(e.Car1, e.Car2);
                }

                if (!lanes.TryUpdate(dependencies))
                {
                    return(e.Time);
                }
            }

            return("Possible");
        }
コード例 #4
0
 void GetInitialCarDependencies(DependencyMatrix matrix)
 {
     // make dependencies
     for (int i = 0; i < cars.Length; i++)
     {
         for (int j = i + 1; j < cars.Length; j++)
         {
             if (AreOverlapping(cars[i].InitialPosition, cars[j].InitialPosition))
             {
                 var ok = matrix.TryAddOvertake(i, j);
                 Debug.Assert(ok);
             }
         }
     }
 }
コード例 #5
0
ファイル: LaneList.cs プロジェクト: yash1998/CodeJam
        public bool TryUpdate(DependencyMatrix dependencies)
        {
            for (int car1 = 0; car1 < lanes.Length; car1++)
            {
                if (dependencies.IsIndependent(car1))
                {
                    lanes[car1] = Lane.Undeterminated;
                    continue;
                }

                foreach (var car2 in dependencies.GetCarsOnSameLane(car1))
                {
                    if (lanes[car2] == Lane.Undeterminated)
                    {
                        continue;
                    }

                    if (lanes[car1] == Lane.Undeterminated)
                    {
                        lanes[car1] = lanes[car2];
                    }
                    else if (lanes[car1] != lanes[car2])
                    {
                        return(false);
                    }
                }

                foreach (var car2 in dependencies.GetCarsOnOppositeLane(car1))
                {
                    if (lanes[car2] == Lane.Undeterminated)
                    {
                        continue;
                    }

                    if (lanes[car1] == Lane.Undeterminated)
                    {
                        lanes[car1] = lanes[car2].Opposite();
                    }
                    else if (lanes[car1] == lanes[car2])
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
コード例 #6
0
        public object Solve()
        {
            var matrix = new DependencyMatrix(cars.Length);
            var lanes  = cars.Select(x => x.InitialLane).ToArray();

            GetInitialCarDependencies(matrix);
            var ok = TryUpdateLaneList(matrix, lanes);

            Debug.Assert(ok);

            var events = EnumerateEvents().OrderBy(x => x.Type).OrderBy(x => x.Time);

            foreach (var e in events)
            {
#if TRACE
                Log("Dependencies:");
                Log("{0}", matrix);
                Log("Lanes:");
                for (var car = 0; car < lanes.Length; car++)
                {
                    Log("{0} = {1}", car, lanes[car]);
                }

                Log("At {3}, event {0} with {1} and {2}", e.Type, e.Car1, e.Car2, e.Time);
#endif


                if (e.Type == EventType.Enter)
                {
                    if (!matrix.TryAddOvertake(e.Car1, e.Car2))
                    {
                        return(e.Time);
                    }
                }
                else
                {
                    matrix.RemoveOvertake(e.Car1, e.Car2);
                }

                if (!TryUpdateLaneList(matrix, lanes))
                {
                    return(e.Time);
                }
            }

            return("Possible");
        }
コード例 #7
0
        public object Solve()
        {
            var matrix = new DependencyMatrix(cars.Length);
            var lanes = cars.Select(x => x.InitialLane).ToArray();

            GetInitialCarDependencies(matrix);
            var ok = TryUpdateLaneList(matrix, lanes);
            Debug.Assert(ok);

            var events = EnumerateEvents().OrderBy(x => x.Type).OrderBy(x => x.Time);

            foreach (var e in events)
            {
            #if TRACE
                Log("Dependencies:");
                Log("{0}", matrix);
                Log("Lanes:");
                for (var car = 0; car < lanes.Length; car++)
                    Log("{0} = {1}", car, lanes[car]);

                Log("At {3}, event {0} with {1} and {2}", e.Type, e.Car1, e.Car2, e.Time);
            #endif

                if (e.Type == EventType.Enter)
                {
                    if (!matrix.TryAddOvertake(e.Car1, e.Car2))
                        return e.Time;
                }
                else
                {
                    matrix.RemoveOvertake(e.Car1, e.Car2);
                }

                if (!TryUpdateLaneList(matrix, lanes))
                    return e.Time;
            }

            return "Possible";
        }
コード例 #8
0
 void GetInitialCarDependencies(DependencyMatrix matrix)
 {
     // make dependencies
     for (int i = 0; i < cars.Length; i++)
     {
         for (int j = i+1; j < cars.Length; j++)
         {
             if (AreOverlapping(cars[i].InitialPosition, cars[j].InitialPosition))
             {
                 var ok = matrix.TryAddOvertake(i, j);
                 Debug.Assert(ok);
             }
         }
     }
 }