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); } } } }
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"); }
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"; }
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); } } } }