private void Traverse(Bridge bridge) { CurrentLocation = bridge.Location; foreach (var observer in _observers) { observer.NotifyLocationChanged(); } //Only hikers whose starting location is before the bridge have to cross it. List <Hiker> hikersTraversing = Hikers.Where(x => x.StartingLocation < bridge.Location).OrderBy(s => s.Speed).ToList(); //Make groups of up to 2 hikers. Pair slowest hikers together to save time. List <List <Hiker> > hikerGroups = hikersTraversing.Select((x, idx) => new { x, idx }) .GroupBy(x => x.idx / 2) .Select(g => g.Select(a => a.x).ToList()).ToList(); foreach (List <Hiker> group in hikerGroups) { MakeTrip(group, bridge); } BridgesCrossed.Add(bridge); foreach (var observer in _observers) { observer.NotifyBridgeCrossed(); } }
private void CheckForHikerExceptions() { if (Hikers == null) { throw new ArgumentNullException("No hikers found."); } if (!Hikers.Any(x => x.StartingLocation == 0)) { throw new ArgumentException("No hikers at the start location."); } if (Hikers.Any(x => x.Speed <= 0)) { throw new ArgumentException("Hiker found with non-positive speed."); } }