Advertisement
krasi1105

12.String Matrix Rotation

Jun 2nd, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. public class Program
  8. {
  9.     public static void Main()
  10.     {
  11.         string input = Console.ReadLine();
  12.         int degrees = int.Parse(Regex.Replace(input, "\\D+", "")) % 360;
  13.         var lines = ReadLines();
  14.  
  15.         int maxLength = lines.Max(l => l.Length);
  16.         int symbolsPerLine = degrees % 180 == 0 ? maxLength : lines.Count;
  17.         int linesCount = degrees % 180 == 0 ? lines.Count : maxLength;
  18.        
  19.         lines = lines
  20.             .Select(l => degrees == 180 || degrees == 270 ? ReverseString(l).PadLeft(maxLength) : l.PadRight(maxLength))
  21.             .ToList();
  22.         if (degrees == 90 || degrees == 180)
  23.         {
  24.             lines.Reverse();
  25.         }
  26.  
  27.         StringBuilder output = new StringBuilder(symbolsPerLine * lines.Count);
  28.         for (int i = 0; i < linesCount; i++)
  29.         {
  30.             for (int j = 0; j < symbolsPerLine; j++)
  31.             {
  32.                 output.Append(degrees % 180 == 0 ? lines[i][j] : lines[j][i]);
  33.             }
  34.             output.AppendLine();
  35.         }
  36.         Console.WriteLine(output.ToString());
  37.     }
  38.  
  39.     private static List<string> ReadLines()
  40.     {
  41.         List<string> lines = new List<string>();
  42.         string input;
  43.         while (!(input = Console.ReadLine()).Equals("END"))
  44.         {
  45.             lines.Add(input);
  46.         }
  47.         return lines;
  48.     }
  49.     private static string ReverseString(string s)
  50.     {
  51.         char[] symbols = s.ToCharArray();
  52.         Array.Reverse(symbols);
  53.         return new string(symbols);
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement