public Roster Run(IEnumerable <Player> players) { var orderedPlayers = players.GroupBy(p => p.Position) .ToDictionary(p => p.Key, g => g.OrderByDescending(p => p.Projection).ToArray()); var roster = new Roster(); roster.Add(orderedPlayers["QB"].First()); roster.Add(orderedPlayers["RB"].Skip(1).First()); roster.Add(orderedPlayers["RB"].Skip(2).First()); roster.Add(orderedPlayers["WR"].Skip(1).First()); roster.Add(orderedPlayers["WR"].Skip(2).First()); roster.Add(orderedPlayers["WR"].Skip(3).First()); roster.Add(orderedPlayers["TE"].First()); roster.Add(orderedPlayers["DST"].First()); var flexOptions = orderedPlayers["RB"].Union(orderedPlayers["WR"]).Union(orderedPlayers["TE"]) .OrderByDescending(p => p.Projection); foreach (var flex in flexOptions) { if (roster.CanAdd(flex)) { roster.Add(flex); break; } } var allTopPlayersOrdered = players .GroupBy(p => p.Position) .SelectMany(g => g.Where(p => p.Projection > Percentile(g, 50))) .OrderByDescending(p => p.Salary).ToArray(); int index = 0; while (roster.Salary > 50000) { var newPlayer = allTopPlayersOrdered[index]; if (!roster.Contains(newPlayer)) { var mostExpensiveAtPosition = roster .Where(p => p.Position == newPlayer.Position) .OrderByDescending(p => p.Salary) .First(); if (mostExpensiveAtPosition.Salary > newPlayer.Salary) { roster.Remove(mostExpensiveAtPosition); roster.Add(newPlayer); } } index++; } return(roster); }
public Roster Run(IEnumerable <Player> players) { var orderedPlayers = players.GroupBy(p => p.Position) .ToDictionary(p => p.Key, g => g.OrderByDescending(p => p.Projection).ToArray()); var perfectRoster = new Roster(); perfectRoster.Add(orderedPlayers["QB"].First()); perfectRoster.Add(orderedPlayers["RB"].Skip(1).First()); perfectRoster.Add(orderedPlayers["RB"].Skip(2).First()); perfectRoster.Add(orderedPlayers["WR"].Skip(1).First()); perfectRoster.Add(orderedPlayers["WR"].Skip(2).First()); perfectRoster.Add(orderedPlayers["WR"].Skip(3).First()); perfectRoster.Add(orderedPlayers["TE"].First()); perfectRoster.Add(orderedPlayers["DST"].First()); var flexOptions = orderedPlayers["RB"].Union(orderedPlayers["WR"]).Union(orderedPlayers["TE"]) .OrderByDescending(p => p.Projection); foreach (var flex in flexOptions) { if (perfectRoster.CanAdd(flex)) { perfectRoster.Add(flex); break; } } var roster = perfectRoster.Clone(); var skips = new List <Player>(); var irreplaceablePositions = new List <string>(); double goal = 50000; while (roster.Salary > goal) { var leastEfficentOnRoster = roster .Where(p => !irreplaceablePositions.Contains(p.Position)) .OrderBy(p => p.PointPerCost) .First(); var newPlayer = players .Where(p => p.Position == leastEfficentOnRoster.Position) .Where(p => p.Salary < leastEfficentOnRoster.Salary) .Except(skips) .OrderByDescending(p => p.Projection) .FirstOrDefault(); if (newPlayer == null) { irreplaceablePositions.Add(leastEfficentOnRoster.Position); } else if (roster.Contains(newPlayer)) { skips.Add(newPlayer); } else { roster.Remove(leastEfficentOnRoster); roster.Add(newPlayer); } } return(roster); }