svetlozar_kirkov

String Matrix Rotation [Advanced C# Practice]

Oct 7th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. namespace StringMatrixRotation
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     internal sealed class App
  8.     {
  9.         internal static void Main()
  10.         {
  11.             string[] rotationInput = Console.ReadLine().Split('(', ')');
  12.             int rotation = int.Parse(rotationInput.ElementAt(rotationInput.Length - 2));
  13.             int numberOfRotations = rotation / 90;
  14.             List<string> lines = new List<string>();
  15.             string currentLine = Console.ReadLine();
  16.            
  17.             while (!currentLine.Equals("END"))
  18.             {
  19.                 lines.Add(currentLine);
  20.                 currentLine = Console.ReadLine();
  21.             }
  22.  
  23.             int longestLineLength = lines.OrderByDescending(s => s.Length).First().Length;
  24.  
  25.             char[,] matrix = new char[lines.Count, longestLineLength];
  26.  
  27.             for (int i = 0; i < lines.Count; i++)
  28.             {
  29.                 if (lines[i].Length < longestLineLength)
  30.                 {
  31.                     lines[i] = lines[i].PadRight(longestLineLength, ' ');
  32.                 }
  33.  
  34.                 for (int j = 0; j < lines[i].Length; j++)
  35.                 {
  36.                     matrix[i, j] = lines[i][j];
  37.                 }
  38.             }
  39.  
  40.             for (int i = 0; i < numberOfRotations; i++)
  41.             {
  42.                 matrix = RotateClockwise(matrix);
  43.             }
  44.  
  45.             for (int i = 0; i < matrix.GetLength(0); i++)
  46.             {
  47.                 for (int j = 0; j < matrix.GetLength(1); j++)
  48.                 {
  49.                     Console.Write(string.Format("{0}", matrix[i, j]));
  50.                 }
  51.  
  52.                 Console.WriteLine();
  53.             }
  54.         }
  55.  
  56.         internal static char[,] RotateClockwise(char[,] matrix)
  57.         {
  58.             int width = matrix.GetLength(0);
  59.             int height = matrix.GetLength(1);
  60.             char[,] rotated = new char[height, width];
  61.             for (int i = 0; i < height; ++i)
  62.             {
  63.                 for (int j = 0; j < width; ++j)
  64.                 {
  65.                     rotated[i, j] = matrix[width - j - 1, i];
  66.                 }
  67.             }
  68.  
  69.             return rotated;
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment