Exemple #1
0
        private static Pot NextGeneration(Pot leftmostPot, Dictionary <byte, bool> spreadRules)
        {
            var runningPattern = new Queue <bool>(new bool[] { false, false, false, false });

            var index = leftmostPot.Index - 2;
            var currentPotPreviousGeneration    = leftmostPot;
            Pot currentPotNextGeneration        = null;
            Pot leftmostPotNextGeneration       = null;
            int?numberOfPotsRemainingToGenerate = null;

            while (true)
            {
                if (numberOfPotsRemainingToGenerate == 0)
                {
                    return(leftmostPotNextGeneration.Trim());
                }
                if (currentPotPreviousGeneration == null)
                {
                    // We are at the end.
                    runningPattern.Enqueue(false);
                    if (numberOfPotsRemainingToGenerate.HasValue)
                    {
                        numberOfPotsRemainingToGenerate--;
                    }
                    else
                    {
                        numberOfPotsRemainingToGenerate = 3;
                    }
                }
                else
                {
                    runningPattern.Enqueue(currentPotPreviousGeneration.HasPlant);
                    currentPotPreviousGeneration = currentPotPreviousGeneration.PotToTheRight;
                }

                var spreadKey = CalculateSpreadKey(runningPattern);
                spreadRules.TryGetValue(spreadKey, out var hasPlant);
                if (currentPotNextGeneration == null)
                {
                    currentPotNextGeneration  = Pot.LeftmostPot(index, hasPlant);
                    leftmostPotNextGeneration = currentPotNextGeneration;
                }
                else
                {
                    currentPotNextGeneration = currentPotNextGeneration.AddToTheRight(hasPlant);
                }

                index++;
                runningPattern.Dequeue();
            }
        }