🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # freddie2025's solution

## to Spiral Matrix in the C# Track

Published at Feb 07 2020 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Given the size, return a square matrix of numbers in spiral order.

The matrix should be filled with natural numbers, starting from 1 in the top-left corner, increasing in an inward, clockwise spiral order, like these examples:

###### Spiral matrix of size 3
``````1 2 3
8 9 4
7 6 5
``````
###### Spiral matrix of size 4
`````` 1  2  3 4
12 13 14 5
11 16 15 6
10  9  8 7
``````

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit SpiralMatrix.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Source

Reddit r/dailyprogrammer challenge #320 [Easy] Spiral Ascension. https://www.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/

### SpiralMatrixTest.cs

``````// This file was auto-generated based on version 1.1.0 of the canonical data.

using Xunit;

public class SpiralMatrixTest
{
[Fact]
public void Empty_spiral()
{
Assert.Empty(SpiralMatrix.GetMatrix(0));
}

[Fact(Skip = "Remove to run test")]
public void Trivial_spiral()
{
var expected = new[,]
{
{ 1 }
};
Assert.Equal(expected, SpiralMatrix.GetMatrix(1));
}

[Fact(Skip = "Remove to run test")]
public void Spiral_of_size_2()
{
var expected = new[,]
{
{ 1, 2 },
{ 4, 3 }
};
Assert.Equal(expected, SpiralMatrix.GetMatrix(2));
}

[Fact(Skip = "Remove to run test")]
public void Spiral_of_size_3()
{
var expected = new[,]
{
{ 1, 2, 3 },
{ 8, 9, 4 },
{ 7, 6, 5 }
};
Assert.Equal(expected, SpiralMatrix.GetMatrix(3));
}

[Fact(Skip = "Remove to run test")]
public void Spiral_of_size_4()
{
var expected = new[,]
{
{ 1, 2, 3, 4 },
{ 12, 13, 14, 5 },
{ 11, 16, 15, 6 },
{ 10, 9, 8, 7 }
};
Assert.Equal(expected, SpiralMatrix.GetMatrix(4));
}

[Fact(Skip = "Remove to run test")]
public void Spiral_of_size_5()
{
var expected = new[,]
{
{ 1, 2, 3, 4, 5 },
{ 16, 17, 18, 19, 6 },
{ 15, 24, 25, 20, 7 },
{ 14, 23, 22, 21, 8 },
{ 13, 12, 11, 10, 9 }
};
Assert.Equal(expected, SpiralMatrix.GetMatrix(5));
}
}``````
``````﻿using System;

public class SpiralMatrix
{
[Flags]
public enum Direction
{
None 	= 0b0000,
Right 	= 0b0001,
Left 	= 0b0010,
Down 	= 0b0100,
Up 		= 0b1000,
All 	= 0b1111,
}

public static int[,] GetMatrix(int size)
{
switch (size)
{
case 0:
return new int[,] {{}};
case 1:
return new int[,] {{1}};
default:

int count = 1, row = 0, col = 0, maxRow = (size -1), maxCol = (size -1), minRow = 1, minCol = 0;
var output = new int[size , size];
var direction = Direction.Right;

output[row, col] = count++;

do
{
switch(direction)
{
case Direction.Right when col == maxCol:
direction = ChangeDirection(direction);
maxCol--;
break;
case Direction.Down when row == maxRow:
direction = ChangeDirection(direction);
maxRow--;
break;
case Direction.Left when col == minCol:
direction = ChangeDirection(direction);
minCol++;
break;
case Direction.Up when row == minRow:
direction = ChangeDirection(direction);
minRow++;
break;
}

switch (direction)
{
case Direction.Right:
col++;
break;
case Direction.Down:
row++;
break;
case Direction.Left:
col--;
break;
case Direction.Up:
row--;
break;
}

output[row, col] = count++;
}
while (count <= Math.Pow(size, 2));

return output;
}

static Direction ChangeDirection(Direction direction)
{
return direction switch
{
Direction.Right => Direction.Down,
Direction.Down => Direction.Left,
Direction.Left => Direction.Up,
Direction.Up => Direction.Right,
_ => Direction.Right
};
}
}
}``````