Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace MatrixShuffle
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- public class Startup
- {
- private static char[,] matrix;
- public static void Main()
- {
- int sizeOfMatrix = int.Parse(Console.ReadLine());
- matrix = new char[sizeOfMatrix, sizeOfMatrix];
- string toFillTheMatrixWith = Console.ReadLine();
- FillTheMatrixInSpiralManner(toFillTheMatrixWith);
- string chessboardText = GetTheChessboardText();
- string result = string.Empty;
- string backgroundColor = string.Empty;
- if (ChessboardTextIsPalyndrome(GetOnlyTheLetters(chessboardText)))
- {
- backgroundColor = "#4FE000";
- }
- else
- {
- backgroundColor = "#E0000F";
- }
- result = $"<div style='background-color:{backgroundColor}'>{chessboardText}</div>";
- Console.WriteLine(result);
- }
- private static string GetOnlyTheLetters(string chessboardText)
- {
- StringBuilder onlyTheLetters = new StringBuilder();
- foreach (char symbol in chessboardText)
- {
- if ((symbol >= 'a' && symbol <= 'z') ||
- (symbol >= 'A' && symbol <= 'Z'))
- {
- onlyTheLetters.Append(symbol);
- }
- }
- return onlyTheLetters.ToString().ToLower();
- }
- private static bool ChessboardTextIsPalyndrome(string chessboardText)
- {
- bool isPalyndrome = true;
- for (int symbol = 0; symbol < chessboardText.Length / 2; symbol++)
- {
- int symbolToCompareWith = chessboardText.Length - 1 - symbol;
- if (chessboardText[symbol] != chessboardText[symbolToCompareWith])
- {
- isPalyndrome = false;
- break;
- }
- }
- return isPalyndrome;
- }
- private static void PrintMatrix()
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write(matrix[row, col]);
- }
- Console.WriteLine();
- }
- }
- private static string GetTheChessboardText()
- {
- StringBuilder fromWhiteSquares = new StringBuilder();
- StringBuilder fromBlackSquares = new StringBuilder();
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- int whiteSquareStart = row % 2 == 0 ? 0 : 1;
- int blackSquareStart = row % 2 == 0 ? 1 : 0;
- for (int col = whiteSquareStart; col < matrix.GetLength(1); col += 2)
- {
- fromWhiteSquares.Append(matrix[row, col]);
- }
- for (int col = blackSquareStart; col < matrix.GetLength(1); col += 2)
- {
- fromBlackSquares.Append(matrix[row, col]);
- }
- }
- StringBuilder result = new StringBuilder();
- result.Append(fromWhiteSquares);
- result.Append(fromBlackSquares);
- return result.ToString();
- }
- private static void FillTheMatrixInSpiralManner(string toFillTheMatrixWith)
- {
- int indexOfString = 0;
- int indexOfDirection = 0;
- Position currPosition = new Position(0, 0);
- Position currDirection = Position.directions[indexOfDirection];
- while (!MatrixIsFilled())
- {
- while (PositionIsValid(currPosition))
- {
- matrix[currPosition.Row, currPosition.Col] = indexOfString < toFillTheMatrixWith.Length ?
- toFillTheMatrixWith[indexOfString] :
- ' ';
- indexOfString++;
- IncrementCurrentPosition(ref currPosition, ref currDirection);
- }
- currPosition.Row -= currDirection.Row;
- currPosition.Col -= currDirection.Col;
- indexOfDirection++;
- currDirection.Row = Position.directions[indexOfDirection % Position.directions.Length].Row;
- currDirection.Col = Position.directions[indexOfDirection % Position.directions.Length].Col;
- IncrementCurrentPosition(ref currPosition, ref currDirection);
- }
- }
- private static void IncrementCurrentPosition(ref Position currPosition, ref Position currDirection)
- {
- currPosition.Row += currDirection.Row;
- currPosition.Col += currDirection.Col;
- }
- private static bool MatrixIsFilled()
- {
- bool isFilled = true;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- if (matrix[row, col] == '\0')
- {
- isFilled = false;
- goto ReturnTheResult;
- }
- }
- }
- ReturnTheResult:
- return isFilled;
- }
- private static bool PositionIsValid(Position currPosition)
- {
- bool isValid = DimensionIsValid(currPosition.Row, 0) &&
- DimensionIsValid(currPosition.Col, 1) &&
- matrix[currPosition.Row, currPosition.Col] == '\0';
- return isValid;
- }
- private static bool DimensionIsValid(int index, int dimension)
- {
- bool isValid = index >= 0 && index < matrix.GetLength(dimension);
- return isValid;
- }
- }
- public struct Position
- {
- public static readonly Position[] directions = new Position[]
- {
- new Position(0, +1), //right
- new Position(+1, 0), //down
- new Position(0, -1), //left
- new Position(-1, 0) //up
- };
- private int row;
- private int col;
- public Position(int row, int col)
- {
- this.row = row;
- this.col = col;
- }
- public int Row
- {
- get { return this.row; }
- set { this.row = value; }
- }
- public int Col
- {
- get { return this.col; }
- set { this.col = value; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement