Advertisement
simonradev

TextBombardment

Mar 31st, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. namespace TextBombardment
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Text;
  6.  
  7.     public class TextBombardment
  8.     {
  9.         public static char[,] matrix;
  10.  
  11.         public static void Main()
  12.         {
  13.             string text = Console.ReadLine();
  14.             double cols = double.Parse(Console.ReadLine());
  15.  
  16.             int rows = (int)Math.Ceiling(text.Length / cols);
  17.  
  18.             matrix = new char[rows, (int)cols];
  19.             FillTheMatrix(text);
  20.  
  21.             int[] colsToBomb = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  22.  
  23.             foreach (int col in colsToBomb)
  24.             {
  25.                 BombTheCol(col);
  26.             }
  27.  
  28.             PrintTheResult(text);
  29.         }
  30.  
  31.         private static void BombTheCol(int col)
  32.         {
  33.             int row = GetTheStartRow(col);
  34.             while (row != matrix.GetLength(0) && matrix[row, col] != ' ')
  35.             {
  36.                 matrix[row, col] = ' ';
  37.  
  38.                 row++;
  39.             }
  40.         }
  41.  
  42.         private static int GetTheStartRow(int col)
  43.         {
  44.             int rowToReturn = 0;
  45.  
  46.             while (rowToReturn != matrix.GetLength(0) && matrix[rowToReturn, col] == ' ')
  47.             {
  48.                 rowToReturn++;
  49.             }
  50.  
  51.             return rowToReturn;
  52.         }
  53.  
  54.         private static void PrintTheResult(string text)
  55.         {
  56.             StringBuilder result = new StringBuilder();
  57.             for (int row = 0; row < matrix.GetLength(0); row++)
  58.             {
  59.                 for (int col = 0; col < matrix.GetLength(1); col++)
  60.                 {
  61.                     result.Append(matrix[row, col]);
  62.  
  63.                     if (result.Length == text.Length)
  64.                     {
  65.                         goto PrintResult;
  66.                     }
  67.                 }
  68.             }
  69.  
  70.             PrintResult:
  71.             Console.WriteLine(result.ToString());
  72.         }
  73.  
  74.         private static void FillTheMatrix(string text)
  75.         {
  76.             int indexOfText = -1;
  77.             for (int row = 0; row < matrix.GetLength(0); row++)
  78.             {
  79.                 for (int col = 0; col < matrix.GetLength(1); col++)
  80.                 {
  81.                     char toFill = ++indexOfText < text.Length ? text[indexOfText] : ' ';
  82.  
  83.                     matrix[row, col] = toFill;
  84.                 }
  85.             }
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement