Example #1
0
        public decimal Grow2(
            string input,
            string inputPatterns)
        {
            var patterns = inputPatterns.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)
                           .Select(p => p.Split(new string[] { " => " }, StringSplitOptions.None))
                           .Select(p => new Pattern(p[0], p[1].ToCharArray()[0]))
                           .ToArray();

            var iteration      = new PlantIteration(input, 1, 0);
            var lastValue      = 0;
            var lastDifference = 0;

            for (var i = 1; i <= 200; i++)
            {
                var newState = iteration.GetNewState(patterns);

                iteration = new PlantIteration(newState, i + 1, iteration.ZeroIndex);
                var newValue = CalculateSum(iteration);
                lastDifference = newValue - lastValue;
                Console.WriteLine($"Iteration: {i} - Sum: {newValue}, Difference: {lastDifference}");
                lastValue = newValue;
            }

            var value = ((50000000000M - 200) * lastDifference) + lastValue;

            return(value);
        }
Example #2
0
        private static int CalculateSum(
            PlantIteration iteration)
        {
            var value = 0;

            for (var i = 0; i < iteration.CurrentState.Length; i++)
            {
                if (iteration.CurrentState[i] == '#')
                {
                    value += i - iteration.ZeroIndex;
                }
            }

            return(value);
        }
Example #3
0
        public int Grow1(
            string input,
            string inputPatterns)
        {
            var patterns = inputPatterns.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)
                           .Select(p => p.Split(new string[] { " => " }, StringSplitOptions.None))
                           .Select(p => new Pattern(p[0], p[1].ToCharArray()[0]))
                           .ToArray();

            var iteration = new PlantIteration(input, 1, 0);

            for (var i = 1; i <= 20; i++)
            {
                var newState = iteration.GetNewState(patterns);
                iteration = new PlantIteration(newState, i + 1, iteration.ZeroIndex);
            }

            var value = CalculateSum(iteration);

            return(value);
        }