Advertisement
simonradev

Crossfire

Mar 18th, 2017
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.65 KB | None | 0 0
  1. namespace Crossfire
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Text;
  6.  
  7.     public class CrossfireEntryPoint
  8.     {
  9.         public static int[][] jaggedArray;
  10.  
  11.         public static void Main()
  12.         {
  13.             int[] dimensions = Console.ReadLine()
  14.                                 .Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  15.                                 .Select(int.Parse)
  16.                                 .ToArray();
  17.  
  18.             int rows = dimensions[0];
  19.             int cols = dimensions[1];
  20.  
  21.             jaggedArray = new int[rows][];
  22.             int fillNumber = 1;
  23.             for (int row = 0; row < rows; row++)
  24.             {
  25.                 jaggedArray[row] = new int[cols];
  26.  
  27.                 int col;
  28.                 for (col = 0; col < cols; col++)
  29.                 {
  30.                     jaggedArray[row][col] = col + fillNumber;
  31.                 }
  32.  
  33.                 fillNumber += col;
  34.             }
  35.  
  36.             string inputLine = Console.ReadLine().Trim();
  37.             while (inputLine != "Nuke it from orbit")
  38.             {
  39.                 string[] commandInfo = inputLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  40.  
  41.                 int row = int.Parse(commandInfo[0]);
  42.                 int col = int.Parse(commandInfo[1]);
  43.                 int radius = int.Parse(commandInfo[2]);
  44.  
  45.                 if (!InputIsValid(row, col))
  46.                 {
  47.                     inputLine = Console.ReadLine().Trim();
  48.  
  49.                     continue;
  50.                 }
  51.  
  52.                 RemoveTheColsElements(row, col, radius);
  53.  
  54.                 RemoveTheElementsFromTheRow(row, col, radius);
  55.  
  56.                 RemoveTheEmptyRowsFromTheJaggedArray();
  57.  
  58.                 inputLine = Console.ReadLine().Trim();
  59.             }
  60.  
  61.             PrintJaggedArray();
  62.         }
  63.  
  64.         private static void RemoveTheEmptyRowsFromTheJaggedArray()
  65.         {
  66.             if (!jaggedArray.Any(a => a.Length == 0))
  67.             {
  68.                 return;
  69.             }
  70.  
  71.             int rowsOfTheNewJaggedArray = 0;
  72.             for (int currRow = 0; currRow < jaggedArray.Length; currRow++)
  73.             {
  74.                 if (jaggedArray[currRow].Length == 0)
  75.                 {
  76.                     continue;
  77.                 }
  78.  
  79.                 rowsOfTheNewJaggedArray++;
  80.             }
  81.  
  82.             int[][] jaggedArrayHolder = new int[rowsOfTheNewJaggedArray][];
  83.             int newArrIndex = 0;
  84.             for (int currRow = 0; currRow < jaggedArray.Length; currRow++)
  85.             {
  86.                 if (jaggedArray[currRow].Length == 0)
  87.                 {
  88.                     continue;
  89.                 }
  90.  
  91.                 jaggedArrayHolder[newArrIndex] = jaggedArray[currRow];
  92.                 newArrIndex++;
  93.             }
  94.  
  95.             jaggedArray = jaggedArrayHolder.ToArray();
  96.         }
  97.  
  98.         private static void RemoveTheElementsFromTheRow(int row, int col, int radius)
  99.         {
  100.             if (row < 0 || row >= jaggedArray.Length)
  101.             {
  102.                 return;
  103.             }
  104.  
  105.             int minCol = (col - radius) >= 0 ? (col - radius) : 0;
  106.             int maxCol = (col + radius) < jaggedArray[row].Length ? (col + radius) : (jaggedArray[row].Length - 1);
  107.  
  108.             if (minCol > maxCol)
  109.             {
  110.                 return;
  111.             }
  112.  
  113.             int start = minCol;
  114.             int count = maxCol - minCol;
  115.  
  116.             int[] arrHolder = jaggedArray[row];
  117.  
  118.             int[] newArray = RemovePortionOfTheArray(arrHolder, start, count);
  119.  
  120.             jaggedArray[row] = newArray;
  121.         }
  122.  
  123.         private static void RemoveTheColsElements(int row, int col, int radius)
  124.         {
  125.             if (col < 0 || col >= jaggedArray.Max(a => a.Length))
  126.             {
  127.                 return;
  128.             }
  129.  
  130.             int minRow = (row - radius) >= 0 ? (row - radius) : 0;
  131.             int maxRow = (row + radius) < jaggedArray.Length ? (row + radius) : (jaggedArray.Length - 1);
  132.  
  133.             for (int currRow = minRow; currRow <= maxRow; currRow++)
  134.             {
  135.                 if (currRow == row)
  136.                 {
  137.                     continue;
  138.                 }
  139.  
  140.                 int[] arrHolder = jaggedArray[currRow];
  141.  
  142.                 if (col >= arrHolder.Length)
  143.                 {
  144.                     continue;
  145.                 }
  146.  
  147.                 int[] newArray = RemovePortionOfTheArray(arrHolder, col, 0);
  148.  
  149.                 jaggedArray[currRow] = newArray;
  150.             }
  151.         }
  152.  
  153.         /// <summary>
  154.         /// Removes portion of the array
  155.         /// </summary>
  156.         /// <param name="arrHolder">The array you want to manipulate</param>
  157.         /// <param name="start">The starting point which is included</param>
  158.         /// <param name="count">The count from the start of the elements you want to remove where 0 is equal to one element</param>
  159.         /// <returns></returns>
  160.         private static int[] RemovePortionOfTheArray(int[] arrHolder, int start, int count)
  161.         {
  162.             int newArrLen = arrHolder.Length - (count + 1);
  163.             int[] toReturn = new int[newArrLen];
  164.  
  165.             int indexOfReturnedArr = 0;
  166.             for (int currElement = 0; currElement < arrHolder.Length; currElement++)
  167.             {
  168.                 if (currElement >= start && currElement <= (start + count))
  169.                 {
  170.                     continue;
  171.                 }
  172.  
  173.                 toReturn[indexOfReturnedArr] = arrHolder[currElement];
  174.  
  175.                 indexOfReturnedArr++;
  176.             }
  177.  
  178.             return toReturn;
  179.         }
  180.  
  181.         private static bool InputIsValid(int row, int col)
  182.         {
  183.             bool toReturn = true;
  184.            
  185.             if ((row < 0 ||
  186.                 row >= jaggedArray.Length) &&
  187.                 (col < 0 ||
  188.                 col >= jaggedArray.Max(a => a.Length)))
  189.             {
  190.                 toReturn = false;
  191.             }
  192.  
  193.             return toReturn;
  194.         }
  195.  
  196.         public static void PrintJaggedArray()
  197.         {
  198.             StringBuilder result = new StringBuilder();
  199.  
  200.             for (int row = 0; row < jaggedArray.Length; row++)
  201.             {
  202.                 for (int col = 0; col < jaggedArray[row].Length; col++)
  203.                 {
  204.                     result.Append(jaggedArray[row][col]);
  205.  
  206.                     if (col + 1 != jaggedArray[row].Length)
  207.                     {
  208.                         result.Append(" ");
  209.                     }
  210.                 }
  211.  
  212.                 if (row + 1 != jaggedArray.Length)
  213.                 {
  214.                     result.AppendLine();
  215.                 }
  216.             }
  217.  
  218.             Console.WriteLine(result.ToString());
  219.         }
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement