Esempio n. 1
0
        private Tuple <int, int> GetNextCoordinate()
        {
            var lastPoint = _spiralGrid.Last();
            var layer     = SpiralGridCalculator.GetLayer(lastPoint.index + 1);

            // Right edge
            if (lastPoint.x == layer)
            {
                // If top corner, go left
                if (lastPoint.y == layer)
                {
                    return(new Tuple <int, int>(lastPoint.x - 1, lastPoint.y));
                }
                // Otherwise, if not bottom corner, right edge goes up
                if (lastPoint.y != -layer)
                {
                    return(new Tuple <int, int>(lastPoint.x, lastPoint.y + 1));
                }
            }
            // Top edge
            if (lastPoint.y == layer)
            {
                // If left corner, go down
                if (lastPoint.x == -layer)
                {
                    return(new Tuple <int, int>(lastPoint.x, lastPoint.y - 1));
                }
                // Otherwise top edge goes left
                return(new Tuple <int, int>(lastPoint.x - 1, lastPoint.y));
            }
            // Left edge
            if (lastPoint.x == -layer)
            {
                // If bottom corner, go right
                if (lastPoint.y == -layer)
                {
                    return(new Tuple <int, int>(lastPoint.x + 1, lastPoint.y));
                }
                // Otherwise, go down
                return(new Tuple <int, int>(lastPoint.x, lastPoint.y - 1));
            }
            // Bottom edge always goes right
            if (lastPoint.y == -layer)
            {
                return(new Tuple <int, int>(lastPoint.x + 1, lastPoint.y));
            }

            throw new Exception("Shouldn't be able to get here");
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Console.Write("Input square number: ");
            var input     = Console.ReadLine();
            var squareNum = int.Parse(input);

            Console.WriteLine(
                "Distance to access point: {0}",
                SpiralGridCalculator.GetManhattanDistanceToSquareOne(squareNum));

            var generator = new AdjacentSumSpiralGridGenerator();

            var value = 0;

            while (value <= squareNum)
            {
                value = generator.GetNextValue();
            }

            Console.WriteLine("First value higher than {0}: {1}", squareNum, value);

            Console.ReadLine();
        }