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.180 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.179 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.178 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.177 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.176 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.175 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.174 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.173 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.172 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.171 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.170 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.169 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.167 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.166 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.165 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.164 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.163 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.162 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.161 tracks/csharp/exercises/spiral-matrix/Example.cs
trackler-2.2.1.160 tracks/csharp/exercises/spiral-matrix/Example.cs