Example #1
0
        public override object SolvePart2(string[] input)
        {
            var previousForrests = new List <Forrest>();
            var forrest          = new Forrest(input);

            previousForrests.Add(forrest);

            var iterations = 1_000_000_000;

            for (var i = 1; i <= iterations; i++)
            {
                forrest = forrest.NextGeneration();

                var sameExistingForrestIndex = previousForrests.IndexOf(forrest);
                if (sameExistingForrestIndex != -1)
                {
                    var repeatCount = i - sameExistingForrestIndex;
                    var remainingIterationsFromFirstRepeat = iterations - sameExistingForrestIndex;

                    var resultForrestIndex = sameExistingForrestIndex + (remainingIterationsFromFirstRepeat % repeatCount);
                    var resultForrest      = previousForrests[resultForrestIndex];


                    return(resultForrest.TreeCount * resultForrest.LumberyardCount);
                }

                previousForrests.Add(forrest);
            }

            throw new Exception("End of the World!");
        }
Example #2
0
        public override object SolvePart1(string[] input)
        {
            var forrest = new Forrest(input);

            for (var i = 1; i <= 10; i++)
            {
                forrest = forrest.NextGeneration();
            }

            return(forrest.TreeCount * forrest.LumberyardCount);
        }