Advertisement
nsavov

Untitled

Jun 5th, 2019
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.01 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. namespace LadyBugs
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             short sizeOfField = short.Parse(Console.ReadLine());    // Field size input
  10.            
  11.             int[] initIndexes = Console.ReadLine()  // Array of the ladybug indexes of the next array
  12.                                 .Split(" ")
  13.                                 .Select(int.Parse)
  14.                                 .ToArray();
  15.            
  16.             int[] ladybugArray = new int[sizeOfField];  // Creating the lady bug array
  17.            
  18.             for (int i = 0; i < initIndexes.Length; i++)    // Filling the ladybug array with 1s on their indexes
  19.             {
  20.                
  21.                
  22.                 if (initIndexes[i] >= 0 &&          // Checking if the current index is in bounds of the ladyBug array
  23.                     initIndexes[i] < sizeOfField)   // to avoid out of bounds exceptions
  24.                 {
  25.                     ladybugArray[initIndexes[i]] = 1;
  26.                 }
  27.             }
  28.            
  29.             string command = Console.ReadLine();    // Getting the input commands -> [ladybugIndex] [left/right] [jumpPlaces] / end
  30.  
  31.             while (command != "end")
  32.             {
  33.                 string[] commandArray = command.Split();    // Splitting the command string into a string array of 3 instructions
  34.                
  35.                 int whichBug = int.Parse(commandArray[0]);  // Parsing the first command index to get the index of the bug as a number
  36.                
  37.                 if (whichBug >= 0 && whichBug < sizeOfField)    // Checking the index of the bug is in bounds to avoid exceptions, but
  38.                 {        
  39.                     int jumpPlaces = int.Parse(commandArray[2]);    // Parsing the last index of the command array to get the jump length
  40.                    
  41.                     if (ladybugArray[whichBug] == 1)    // Checking if there's a bug in the index we are trying to move
  42.                     {
  43.                         if (commandArray[1] == "left")  // Checking the middle command
  44.                         {
  45.                             int jumpCount = 1;  // Initializing the number of jumps - in case landing index is occupied
  46.                            
  47.                             int currLanding = whichBug - jumpPlaces * jumpCount;        // Initializing the current landing index
  48.                            
  49.                             while (currLanding >= 0 && ladybugArray[currLanding] == 1)  // Checking if we are inside the array and if the landing index is occupied
  50.                             {
  51.                                 if (jumpPlaces == 0) break; // Breaking the loop if we jump 0 spaces to avoid perma-loop
  52.                                
  53.                                 jumpCount++;    // Increasing our jump multiplier if landing index is occupied
  54.                                
  55.                                 currLanding = whichBug - jumpPlaces * jumpCount;    // Setting the new land index
  56.                             }
  57.  
  58.                            
  59.                            
  60.                             if (currLanding >= 0)   // Checking if we are still in bounds, otherwise we dont need to change the landing value
  61.                             {
  62.                                 ladybugArray[currLanding] = 1;
  63.                             }
  64.                            
  65.                             if (jumpPlaces != 0)    // Checking if we jumped 0 spaces to avoid nulling our current value
  66.                             {
  67.                                 ladybugArray[whichBug] = 0;
  68.                             }
  69.                         }
  70.  
  71.                         // Same logic like in "left" but reversed
  72.                         else if (commandArray[1] == "right")    
  73.                         {
  74.                             int jumpCount = 1;
  75.                             int currLanding = whichBug + jumpPlaces * jumpCount;
  76.  
  77.                             while (currLanding < sizeOfField && ladybugArray[currLanding] == 1)
  78.                             {
  79.                                 if (jumpPlaces == 0) break;
  80.  
  81.                                 jumpCount++;
  82.                                 currLanding = whichBug + jumpPlaces * jumpCount;
  83.                             }
  84.  
  85.                             if (currLanding < sizeOfField)
  86.                             {
  87.                                 ladybugArray[currLanding] = 1;
  88.                             }
  89.  
  90.                             if (jumpPlaces != 0)
  91.                             {
  92.                                 ladybugArray[whichBug] = 0;
  93.                             }
  94.                         }
  95.                     }
  96.                 }
  97.  
  98.                 // Entering the new command in the end of the current cycle
  99.                 command = Console.ReadLine();
  100.  
  101.             }
  102.  
  103.             // Writting the ladybug array split by spaces
  104.             Console.WriteLine(string.Join(" ", ladybugArray));
  105.  
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement