Challenge #169 - davidwhitney

CSharp · Console

/Program.cs

using System;
using NUnit.Framework;

namespace Kata
{
    public static class Program
    {
        public static void Main()
        {
            var tests = new Tests();

            Console.WriteLine("ThreeByThree_Rotate90");
            tests.SetUp();
            tests.ThreeByThree_Rotate90();

            Console.WriteLine("\r\n\r\nThreeByThree_Rotate180");
            tests.SetUp();
            tests.ThreeByThree_Rotate180();

            Console.WriteLine("\r\n\r\nChallange");
            tests.SetUp();
            tests.Challange(90);
            tests.Challange(180);
            tests.Challange(270);
        }
    }

    public static class ArrayRotation
    {
        public static int[,] Rotate(int degrees, int[,] input)
        {
            var iterations = degrees / 90;

            var xWidth = input.GetLength(0);
            var yWidth = input.GetLength(1);
            var output = new int[yWidth, xWidth];

            while (iterations > 0)
            {
                output = new int[yWidth, xWidth];
                var currentXToPopulate = 0;

                for (var xIndex = xWidth - 1; xIndex >= 0; xIndex--)
                {
                    for (var yIndex = 0; yIndex < yWidth; yIndex++)
                    {
                        output[yIndex, currentXToPopulate] = input[xIndex, yIndex];
                    }

                    currentXToPopulate++;
                }

                iterations--;
                input = output;
            }

            return output;
        }
    }
    
    [TestFixture]
    public class Tests
    {
        private int[,] _input;

        [SetUp]
        public void SetUp()
        {
            _input = new[,]
            {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
            };
        }

        [Test]
        public void ThreeByThree_Rotate90()
        {
            var output = ArrayRotation.Rotate(90, _input);
            PrettyPrint(output);

            Assert.That(output, Is.EqualTo(new[,]
            {
                {7, 4, 1},
                {8, 5, 2},
                {9, 6, 3}
            }));
        }

        [Test]
        public void ThreeByThree_Rotate180()
        {
            var output = ArrayRotation.Rotate(180, _input);
            PrettyPrint(output);

            Assert.That(output, Is.EqualTo(new[,]
            {
                {9, 8, 7},
                {6, 5, 4},
                {3, 2, 1}
            }));
        }

        [TestCase(90)]
        [TestCase(180)]
        [TestCase(270)]
        public void Challange(int degrees)
        {
            Console.WriteLine("\r\nRotating by " + degrees + " degrees");
            
            var input = new[,]
            {
                {1, 2, 3, 4, 5, 6, 7, 8, 9, 0},
                {0, 9, 8, 7, 6, 5, 4, 3, 2, 1},
                {1, 3, 5, 7, 9, 2, 4, 6, 8, 0},
                {0, 8, 6, 4, 2, 9, 7, 5, 3, 1},
                {0, 1, 2, 3, 4, 5, 4, 3, 2, 1},
                {9, 8, 7, 6, 5, 6, 7, 8, 9, 0},
                {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                {2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
                {9, 8, 7, 6, 7, 8, 9, 8, 7, 6},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            };

            var output = ArrayRotation.Rotate(degrees, input);

            PrettyPrint(output);
        }


        public static void PrettyPrint(int[,] array)
        {
            for (int x = 0; x < array.GetLength(0); x += 1)
            {
                for (int y = 0; y < array.GetLength(1); y += 1)
                {
                    Console.Write(array[x, y] + ", ");
                }
                Console.WriteLine();
            }
        }
    }
}
;