Ejemplo n.º 1
0
        public void FlipTestTopOnly(string input, string expectedHash)
        {
            var pancakeStack = new PancakeStack(input);

            var newPancakeStack = PancakeFlipper.Flip(1, pancakeStack);

            Assert.AreEqual(expectedHash, newPancakeStack.GetHash());
            Assert.AreEqual(1, newPancakeStack.Depth);
        }
Ejemplo n.º 2
0
        public PancakeStack FindOptimalStack(IEnumerable <PancakeStack> stacks)
        {
            var pancakeStacks = stacks as PancakeStack[] ?? stacks.ToArray();

            logger.Debug("Evaluating {0} stacks", pancakeStacks.Length);

            // Reach end of recursion if there are no more options to check
            if (!pancakeStacks.Any())
            {
                return(null);
            }

            var NextStacksToCheck = new List <PancakeStack>();

            // Return any if we are ready

            var minPartiontion = pancakeStacks[0].Stack.Length;

            foreach (var pancakeStack in pancakeStacks.OrderBy(x => x.GetPartitions()))
            {
                if (pancakeStack.ReadyToServe())
                {
                    logger.Debug("Found best stack, {0} optimal flips, Orderd {1}", pancakeStack.Depth, pancakeStack.FlipOrder);
                    return(pancakeStack);
                }

                if (pancakeStack.GetPartitions() < minPartiontion)
                {
                    minPartiontion = pancakeStack.GetPartitions();
                }
            }

            logger.Debug("Minimum partions is {0}", minPartiontion);

            // Not Good stack, so let's start flipping each stack we have that is a step in the correct direction
            foreach (var pancakeStack in pancakeStacks.Where(x => x.GetPartitions() == minPartiontion))
            {
                // Keeping track of stacks that have already generated children stacks
                Hashes.Add(pancakeStack.GetHash());

                // Flip all possible ways
                for (var i = 1; i <= pancakeStack.Stack.Length; i++)
                {
                    var flippedStack = PancakeFlipper.Flip(i, pancakeStack);

                    // More Entropy probably means we aren't going in the right direction
                    if (flippedStack.GetPartitions() > pancakeStack.GetPartitions())
                    {
                        continue;
                    }

                    // If this is the first time we see the stack, we'll check out all it's options
                    if (!Hashes.Contains(flippedStack.GetHash()))
                    {
                        NextStacksToCheck.Add(flippedStack);
                        break;
                    }
                }
            }


            return(FindOptimalStack(NextStacksToCheck));
        }