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.38 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.37 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.36 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.35 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.34 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.33 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.32 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.31 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.30 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.29 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.28 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.27 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.26 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.25 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.24 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.23 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.22 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.21 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.20 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.19 tracks/csharp/exercises/spiral-matrix/Example.cs