Advertisement
simonradev

MatrixShuffle

May 16th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.85 KB | None | 0 0
  1. namespace MatrixShuffle
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using System.Threading.Tasks;
  8.    
  9.     public class Startup
  10.     {
  11.         private static char[,] matrix;
  12.  
  13.         public static void Main()
  14.         {
  15.             int sizeOfMatrix = int.Parse(Console.ReadLine());
  16.  
  17.             matrix = new char[sizeOfMatrix, sizeOfMatrix];
  18.  
  19.             string toFillTheMatrixWith = Console.ReadLine();
  20.  
  21.             FillTheMatrixInSpiralManner(toFillTheMatrixWith);
  22.  
  23.             string chessboardText = GetTheChessboardText();
  24.  
  25.             string result = string.Empty;
  26.             string backgroundColor = string.Empty;
  27.             if (ChessboardTextIsPalyndrome(GetOnlyTheLetters(chessboardText)))
  28.             {
  29.                 backgroundColor = "#4FE000";
  30.             }
  31.             else
  32.             {
  33.                 backgroundColor = "#E0000F";
  34.             }
  35.  
  36.             result = $"<div style='background-color:{backgroundColor}'>{chessboardText}</div>";
  37.  
  38.             Console.WriteLine(result);
  39.         }
  40.  
  41.         private static string GetOnlyTheLetters(string chessboardText)
  42.         {
  43.             StringBuilder onlyTheLetters = new StringBuilder();
  44.  
  45.             foreach (char symbol in chessboardText)
  46.             {
  47.                 if ((symbol >= 'a' && symbol <= 'z') ||
  48.                     (symbol >= 'A' && symbol <= 'Z'))
  49.                 {
  50.                     onlyTheLetters.Append(symbol);
  51.                 }
  52.             }
  53.  
  54.             return onlyTheLetters.ToString().ToLower();
  55.         }
  56.  
  57.         private static bool ChessboardTextIsPalyndrome(string chessboardText)
  58.         {
  59.             bool isPalyndrome = true;
  60.  
  61.             for (int symbol = 0; symbol < chessboardText.Length / 2; symbol++)
  62.             {
  63.                 int symbolToCompareWith = chessboardText.Length - 1 - symbol;
  64.                
  65.                 if (chessboardText[symbol] != chessboardText[symbolToCompareWith])
  66.                 {
  67.                     isPalyndrome = false;
  68.  
  69.                     break;
  70.                 }
  71.             }
  72.  
  73.             return isPalyndrome;
  74.         }
  75.  
  76.         private static void PrintMatrix()
  77.         {
  78.             for (int row = 0; row < matrix.GetLength(0); row++)
  79.             {
  80.                 for (int col = 0; col < matrix.GetLength(1); col++)
  81.                 {
  82.                     Console.Write(matrix[row, col]);
  83.                 }
  84.  
  85.                 Console.WriteLine();
  86.             }
  87.         }
  88.  
  89.         private static string GetTheChessboardText()
  90.         {
  91.             StringBuilder fromWhiteSquares = new StringBuilder();
  92.             StringBuilder fromBlackSquares = new StringBuilder();
  93.            
  94.             for (int row = 0; row < matrix.GetLength(0); row++)
  95.             {
  96.                 int whiteSquareStart = row % 2 == 0 ? 0 : 1;
  97.                 int blackSquareStart = row % 2 == 0 ? 1 : 0;
  98.  
  99.                 for (int col = whiteSquareStart; col < matrix.GetLength(1); col += 2)
  100.                 {
  101.                     fromWhiteSquares.Append(matrix[row, col]);
  102.                 }
  103.  
  104.                 for (int col = blackSquareStart; col < matrix.GetLength(1); col += 2)
  105.                 {
  106.                     fromBlackSquares.Append(matrix[row, col]);
  107.                 }
  108.             }
  109.  
  110.             StringBuilder result = new StringBuilder();
  111.             result.Append(fromWhiteSquares);
  112.             result.Append(fromBlackSquares);
  113.  
  114.             return result.ToString();
  115.         }
  116.  
  117.         private static void FillTheMatrixInSpiralManner(string toFillTheMatrixWith)
  118.         {
  119.             int indexOfString = 0;
  120.             int indexOfDirection = 0;
  121.  
  122.             Position currPosition = new Position(0, 0);
  123.             Position currDirection = Position.directions[indexOfDirection];
  124.  
  125.             while (!MatrixIsFilled())
  126.             {
  127.                 while (PositionIsValid(currPosition))
  128.                 {
  129.                     matrix[currPosition.Row, currPosition.Col] = indexOfString < toFillTheMatrixWith.Length ?
  130.                                                                  toFillTheMatrixWith[indexOfString] :
  131.                                                                  ' ';
  132.  
  133.                     indexOfString++;
  134.  
  135.                     IncrementCurrentPosition(ref currPosition, ref currDirection);
  136.                 }
  137.  
  138.                 currPosition.Row -= currDirection.Row;
  139.                 currPosition.Col -= currDirection.Col;
  140.  
  141.                 indexOfDirection++;
  142.  
  143.                 currDirection.Row = Position.directions[indexOfDirection % Position.directions.Length].Row;
  144.                 currDirection.Col = Position.directions[indexOfDirection % Position.directions.Length].Col;
  145.  
  146.                 IncrementCurrentPosition(ref currPosition, ref currDirection);
  147.             }
  148.         }
  149.  
  150.         private static void IncrementCurrentPosition(ref Position currPosition, ref Position currDirection)
  151.         {
  152.             currPosition.Row += currDirection.Row;
  153.             currPosition.Col += currDirection.Col;
  154.         }
  155.  
  156.         private static bool MatrixIsFilled()
  157.         {
  158.             bool isFilled = true;
  159.  
  160.             for (int row = 0; row < matrix.GetLength(0); row++)
  161.             {
  162.                 for (int col = 0; col < matrix.GetLength(1); col++)
  163.                 {
  164.                     if (matrix[row, col] == '\0')
  165.                     {
  166.                         isFilled = false;
  167.  
  168.                         goto ReturnTheResult;
  169.                     }
  170.                 }
  171.             }
  172.  
  173.             ReturnTheResult:
  174.             return isFilled;
  175.         }
  176.  
  177.         private static bool PositionIsValid(Position currPosition)
  178.         {
  179.             bool isValid = DimensionIsValid(currPosition.Row, 0) &&
  180.                            DimensionIsValid(currPosition.Col, 1) &&
  181.                            matrix[currPosition.Row, currPosition.Col] == '\0';
  182.  
  183.             return isValid;
  184.         }
  185.  
  186.         private static bool DimensionIsValid(int index, int dimension)
  187.         {
  188.             bool isValid = index >= 0 && index < matrix.GetLength(dimension);
  189.  
  190.             return isValid;
  191.         }
  192.     }
  193.  
  194.     public struct Position
  195.     {
  196.         public static readonly Position[] directions = new Position[]
  197.         {
  198.             new Position(0, +1), //right
  199.             new Position(+1, 0), //down
  200.             new Position(0, -1), //left
  201.             new Position(-1, 0)  //up
  202.         };
  203.  
  204.         private int row;
  205.         private int col;
  206.  
  207.         public Position(int row, int col)
  208.         {
  209.             this.row = row;
  210.             this.col = col;
  211.         }
  212.  
  213.         public int Row
  214.         {
  215.             get { return this.row; }
  216.             set { this.row = value; }
  217.         }
  218.  
  219.         public int Col
  220.         {
  221.             get { return this.col; }
  222.             set { this.col = value; }
  223.         }
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement