Пример #1
0
        private static Pot ParseInitialState(string initialState)
        {
            var pot0         = Pot.LeftmostPot(0, initialState[15] == '#');
            var rightmostPot = pot0;

            foreach (var pot in initialState.Skip(16))
            {
                rightmostPot = rightmostPot.AddToTheRight(pot == '#');
            }

            return(pot0);
        }
Пример #2
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();
            }
        }