public Tuple <List <int>, List <int> > FindPermutation(SlowBoard other) { if (!Degrees.Select(x => x.Item2).SequenceEqual(other.Degrees.Select(x => x.Item2))) { return(null); } var mappingPairs = new List <Tuple <List <int>, List <int> > >(); for (int i = 0; i < DegreeChangeIndices.Count - 1; i++) { var domain = new List <int>(); var codomain = new List <int>(); for (int j = DegreeChangeIndices[i]; j < DegreeChangeIndices[i + 1]; j++) { domain.Add(Degrees[j].Item1); codomain.Add(other.Degrees[j].Item1); } mappingPairs.Add(new Tuple <List <int>, List <int> >(domain, codomain)); } return(mappingPairs.Select(mp => Permutation.EnumerateAll(mp.Item2.Count).Select(p => new Tuple <List <int>, List <int> >(mp.Item1, p.Apply(mp.Item2)))) .CartesianProduct() .Select(mapping => FlattenMapping(mapping)) .FirstOrDefault(mapping => SequenceEqualPermuted(mapping, other))); }