public override int GetHashCode() { return(Toy != null ? Toy.GetHashCode() : 0); }
public static ToyDistributionProblem Create() { while (true) { Toy[] toys = ToyNames.Select(x => new Toy(x)).ToArray(); Dictionary <string, Toy> toysDictionary = toys.ToDictionary(x => x.Name); Child[] children = Names.Shuffle() .Take(ListLength) .Select(x => new Child(x, GetRandomWishList(toysDictionary.Select(t => t.Value).ToArray()))) .ToArray(); bool InternalCreate(out Child[] randomChildren1, out List <Toy> list) { randomChildren1 = children; InterestingToy[] byInterest = InterestingToys(randomChildren1); if (byInterest.Take(5).Any(x => x.Counter == 1)) { randomChildren1 = null; list = null; return(false); } List <Toy> interestingToys = byInterest.SkipWhile(x => x.Counter > 2).Select(x => x.Toy).ToList(); list = new List <Toy>(ListLength); foreach (Child c in randomChildren1.OrderBy(x => x.WishList.Toys.Count)) { Toy first = c.WishList.Toys.FirstOrDefault(interestingToys.Contains); if (first == null) { return(false); } interestingToys.Remove(first); list.Add(first); } return(true); } if (InternalCreate(out Child[] randomChildren, out List <Toy> toyPool)) { var problem = new ToyDistributionProblem { Children = randomChildren.Shuffle().ToArray(), Toys = toyPool.Shuffle().ToArray(), }; ToyDistributionProblem originalProblem = problem.DeepClone(); ToyDistributionSolution solution = problem.CreateSolution(); if (solution != null && solution.IsValidFor(originalProblem)) { return(originalProblem); } } // try again :D } }
public InterestingToy(Toy toy) { Toy = toy; }