Sha256: 568973c22ef0928ba84f160a5a53c59ab072785f91ba303eec3ea48a2a784f58

Contents?: true

Size: 1.02 KB

Versions: 168

Compression:

Stored size: 1.02 KB

Contents

public static class SpiralMatrix
{
    public static int[,] GetMatrix(int size)
    {
        var spiral = new int[size, size];
        int numbersToPlace = size * size;

        int currentSpiralValue = 1;
        int firstPivot = 0, secondPivot = size - 1;

        while (currentSpiralValue <= numbersToPlace)
        {
            for (int i = firstPivot; i <= secondPivot; i++)
            {
                spiral[firstPivot, i] = currentSpiralValue++;
            }

            for (int j = firstPivot + 1; j <= secondPivot; j++)
            {
                spiral[j, secondPivot] = currentSpiralValue++;
            }

            for (int i = secondPivot - 1; i >= firstPivot; i--)
            {
                spiral[secondPivot, i] = currentSpiralValue++;
            }

            for (int j = secondPivot - 1; j >= firstPivot + 1; j--)
            {
                spiral[j, firstPivot] = currentSpiralValue++;
            }

            firstPivot++;
            secondPivot--;
        }

        return spiral;
    }
}

Version data entries

168 entries across 168 versions & 1 rubygems

Version Path
trackler-2.2.1.18 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.17 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.16 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.15 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.14 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.13 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.12 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.11 tracks/csharp/exercises/spiral-matrix/Example.cs