Ejemplo n.º 1
0
        internal void Run()
        {
            var digits = Enumerable.Range(0, 10).ToArray();
            var cmb    = new Combinatorics();
            var maxSol = 0L;

            cmb.GeneratePermutations(digits, 10, arr => {
                var number = new string(arr.Select(c => (char)('0' + c)).ToArray());
                var check  = true;
                for (var i = 0; i < arr.Length - 1; i++)
                {
                    var neighborSum = (arr[i] + arr[i + 1]).ToString();
                    if (!number.Contains(neighborSum))
                    {
                        check = false;
                        break;
                    }
                }
                if (check)
                {
                    var sol = Int64.Parse(number);
                    if (sol > maxSol)
                    {
                        maxSol = sol;
                    }
                }
            }, (candidate, countSoFar, partialSolution) => {
                return(countSoFar > 0 || candidate != 0);
            });
            Console.WriteLine(maxSol);
            return;
        }
Ejemplo n.º 2
0
        internal void Run()
        {
            var solutions     = new List <string>();
            int boardSize     = 5;
            var rowPlacements = new int[] { 1, 2, 19, 4, 21, 22, 7, 8, 25, 26, 11, 28, 13, 14, 31, 16 };
            //var rowPlacements = new int[] { 8,1, 2, 11, 4, 13, 14, 7};
            //var rowPlacements = new int[] { 1, 2, 4, 7};
            var pawnPlacements = rowPlacements.Select(encoded => new pawnPlacement()
            {
                placement      = encoded,
                bitMap         = getBitmap(encoded, boardSize),
                representation = writeOut(getBitmap(encoded, boardSize))
            }).ToDictionary(pp => pp.placement, pp => pp);
            var cmb = new Combinatorics();

            cmb.GenerateAllTuples(rowPlacements, boardSize, partialSol => {
                if (columnCheck(partialSol, boardSize, pawnPlacements))
                {
                    var board = string.Join("\n", partialSol.Select(row => pawnPlacements[row].representation));
                    solutions.Add(board);
                    Console.WriteLine(board);
                }
            }, (x, y, z) => true);

            return;
        }
Ejemplo n.º 3
0
        internal void Run()
        {
            var ballcount  = 12;
            var targetInfo = (ballcount * (ballcount - 1)) / 2;
            var balls      = Enumerable.Range(0, ballcount).Select(chrOffset => (char)('a' + chrOffset)).ToArray();
            var cmb        = new Combinatorics();
            var picks      = new Dictionary <string, char[]>();

            cmb.GenerateCombinations(balls, 4, result =>
            {
                var str    = new string(result);
                picks[str] = result.Take(4).ToArray();
            }, (candidate, count, partialResult) => true);
            var knownInfo = new HashSet <weigh>();
            var counter   = 1;

            while (knownInfo.Count < targetInfo)
            {
                var weighInfos = picks.ToDictionary(kvp => kvp.Key, kvp => getRevealedInfo(kvp.Value, knownInfo));
                var bestWeigh  = picks[weighInfos.OrderByDescending(kvp => kvp.Value).FirstOrDefault().Key];
                markBestWeigh(bestWeigh, knownInfo);
                Console.WriteLine("{0}-{1}", counter, new string(bestWeigh));
                counter++;
            }
            return;
        }
Ejemplo n.º 4
0
        internal void Run()
        {
            var alphabet = new char[] { 'A', 'B' };
            var cmb      = new Combinatorics();

            for (int i = 3; i < 32; i++)
            {
                var win       = 0;
                var lose      = 0;
                var undecided = 0;
                cmb.GenerateAllTuples(alphabet, i, prt => {
                    var str       = new string(prt);
                    var firstABA  = str.IndexOf("ABA");
                    var firstBABA = str.IndexOf("BABA");
                    if (firstABA < 0 && firstBABA < 0)
                    {
                        undecided++;
                    }
                    else
                    {
                        if (firstABA >= 0 && (firstBABA < 0 || firstABA < firstBABA))
                        {
                            win++;
                        }
                        else
                        {
                            lose++;
                        }
                    }

                    return;
                }, (x, y, z) => true);
                Console.WriteLine("W:{0} L:{1} U:{2} T:{3}", win, lose, undecided, win + lose + undecided);
            }
        }
Ejemplo n.º 5
0
        internal void Run()
        {
            var boardSize = 4;
            var squares   = Enumerable.Range(0, boardSize * boardSize).Select(ordinal => getCoord(ordinal, boardSize)).ToArray();
            var cmb       = new Combinatorics();
            var solutions = new List <coord[]>();

            cmb.GenerateCombinations(squares, 8, result => solutions.Add(result.ToArray()), (candidate, countSoFar, partialSolution) => checkCandidate(candidate, partialSolution.Take(countSoFar).ToArray()));
            foreach (var solution in solutions)
            {
                printSolution(solution, boardSize);
                Console.WriteLine("----");
            }
            return;
        }
Ejemplo n.º 6
0
        internal void Run()
        {
            var alphabet       = new char[] { 'A', 'B' };
            var cmb            = new Combinatorics();
            int neverLostCount = 0;

            cmb.GenerateAllTuples(alphabet, 14, registerScore, (canddidate, count, partialSolution) => true);
            foreach (var score in scores)
            {
                var neverLost = testLoss(score);
                if (neverLost)
                {
                    neverLostCount++;
                }
                Console.WriteLine("{0}:{1}", new string(score), neverLost?"GOOD":"BAD");
            }
            return;
        }
Ejemplo n.º 7
0
 internal void Run()
 {
     List<int[]> combos = new List<int[]>();
     var cmb = new Combinatorics();
     var numbers = Enumerable.Range(1, 20).ToArray();
     cmb.GenerateCombinations(numbers, 9, combo =>
     {
         if (combo.Sum() == 51)
         {
             combos.Add(combo.ToArray());
         }
     }, (itm, idx, partialCombo) => true);
     foreach (var combo in combos)
     {
         cmb.GeneratePermutations(combo, 9, result =>
          {
              return;
          }
         , (candidate, position, partialSolution) =>
          checkForSquareMagicSquare(candidate, position, partialSolution));
     }
     return;
 }
Ejemplo n.º 8
0
        internal void Run()
        {
            int counter     = 0;
            var c           = new Combinatorics();
            int letterCount = 7;
            var letters     = Enumerable.Range(0, letterCount).Select(offset => (char)(AChar + offset)).ToArray();

            var envelopments = new List <string>();

            c.GenerateAllTuples(letters, 3, tuple =>
            {
                var x = new string(tuple);
                envelopments.Add(x);
            }, checkWrongEnvelope);
            //Console.WriteLine(String.Join("\r\n", envelopments));
            c.GeneratePermutations(envelopments.ToArray(), letterCount, tuple =>
            {
                var y = string.Join(",", tuple);
                //Console.WriteLine(y);
                counter++;
                Debug.Assert(counter <= 3437316);
            }, checkEnvelopePosition);
            Console.WriteLine(counter);
        }