예제 #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!");
        }
예제 #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);
        }
예제 #3
0
        protected bool Equals(Forrest other)
        {
            if (!(SizeX == other.SizeX && SizeY == other.SizeY))
            {
                return(false);
            }

            for (var i = 0; i < SizeX * SizeY; i++)
            {
                if (_tiles[i] != other._tiles[i])
                {
                    return(false);
                }
            }

            return(true);
        }