Simooo

Lego Blocks

Aug 27th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.64 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class LegoBlocks
  5. {
  6.  
  7.     static int[] FillJaggedArrayRow(string[] row)
  8.     {
  9.         int[] array = new int[row.Length];
  10.         for (int j = 0; j < row.Length; j++)
  11.         {
  12.             array[j] = int.Parse(row[j]);
  13.         }
  14.         return array;
  15.     }
  16.     static int[] ReversingArray(int[] jaggedArrayTwoRow)
  17.     {
  18.         for (int i = 0; i < jaggedArrayTwoRow.Length / 2; i++)
  19.         {
  20.             int container = jaggedArrayTwoRow[i];
  21.             jaggedArrayTwoRow[i] = jaggedArrayTwoRow[jaggedArrayTwoRow.Length - i - 1];
  22.             jaggedArrayTwoRow[jaggedArrayTwoRow.Length - i - 1] = container;
  23.         }
  24.         return jaggedArrayTwoRow;
  25.     }
  26.  
  27.     private static bool CheckingIfTheArraysCanConnect(int[][] jaggedArrayOne, int[][] jaggedArrayTwo)
  28.     {
  29.         bool arraysCanConnect = true;
  30.         int lastRowLength = -1;
  31.         for (int i = 0; i < jaggedArrayOne.Length; i++)
  32.         {
  33.             int currentRowLength = jaggedArrayOne[i].Length + jaggedArrayTwo[i].Length;
  34.             if(arraysCanConnect == false )
  35.             {
  36.                 break;
  37.             }
  38.             if((currentRowLength != lastRowLength) && lastRowLength > -1)
  39.             {
  40.                 arraysCanConnect = false;
  41.             }
  42.  
  43.             lastRowLength = jaggedArrayOne[i].Length + jaggedArrayTwo[i].Length;
  44.         }
  45.         return arraysCanConnect;
  46.     }
  47.  
  48.     private static void PrintingResult(bool theJaggedArraysCanBeConnected, int[][] jaggedArrayOne, int[][] jaggedArrayTwo)
  49.     {
  50.        
  51.         if(theJaggedArraysCanBeConnected == true)
  52.         {
  53.             for (int i = 0; i < jaggedArrayOne.Length; i++)
  54.             {
  55.                 Console.WriteLine('[' + string.Join(", ", jaggedArrayOne[i]) + ", " + string.Join(", ", jaggedArrayTwo[i]) + ']');
  56.             }
  57.         }
  58.         else
  59.         {
  60.             int numberOfCells = 0;
  61.             for (int i = 0; i < jaggedArrayOne.Length; i++)
  62.             {
  63.                 foreach (var number in jaggedArrayOne[i])
  64.                 {
  65.                     numberOfCells++;
  66.                 }
  67.                 foreach (var number in jaggedArrayTwo[i])
  68.                 {
  69.                     numberOfCells++;
  70.                 }
  71.             }
  72.             Console.WriteLine("The total number of cells is: {0}", numberOfCells);
  73.         }
  74.     }
  75.     static void Main()
  76.     {
  77.         // You are given two jagged arrays. Each array represents a Lego block containing integers. Your task is first to reverse the second jagged array and then check if it would fit perfectly in the first jagged array.
  78.  
  79.         int rows = int.Parse(Console.ReadLine());
  80.         int[][] jaggedArrayOne = new int[rows][];
  81.         int[][] jaggedArrayTwo = new int[rows][];
  82.         int index = 0;
  83.         // Filling the jaggedArrays
  84.         for (int i = 0; i < 2 * rows; i++)
  85.         {
  86.             string[] row = Console.ReadLine().Split(' ');
  87.             if (i < rows)
  88.             {
  89.                 jaggedArrayOne[index] = FillJaggedArrayRow(row);
  90.             }
  91.             else
  92.             {
  93.                 jaggedArrayTwo[index] = FillJaggedArrayRow(row);
  94.                 jaggedArrayTwo[index] = ReversingArray(jaggedArrayTwo[index]);
  95.             }
  96.  
  97.             index++;
  98.             if (index == rows)
  99.             {
  100.                 index = 0;
  101.             }
  102.         }
  103.         // initialising a new jagged array to connect the first two
  104.        
  105.        bool theJaggedArraysCanBeConnected = CheckingIfTheArraysCanConnect(jaggedArrayOne, jaggedArrayTwo);
  106.        // Printing result
  107.        PrintingResult(theJaggedArraysCanBeConnected, jaggedArrayOne, jaggedArrayTwo);
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment