Guest User

Untitled

a guest
Jan 18th, 2017
1,369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         int sizeField = int.Parse(Console.ReadLine());
  9.         int[] indexesToPlantBugs = Console.ReadLine().Split().Select(int.Parse).ToArray();
  10.         string[] commandInput = Console.ReadLine().Split();
  11.  
  12.         int[] field = PlantBugsOnField(sizeField, indexesToPlantBugs);
  13.        
  14.         while (commandInput[0] != "end")
  15.         {
  16.             int bugIndex = int.Parse(commandInput[0]);
  17.             string flightDirection = commandInput[1];
  18.             int flightLength = int.Parse(commandInput[2]);
  19.  
  20.             flightDirection = FlightDirectionCorrection(flightDirection, flightLength);
  21.  
  22.             //Check for misleading index input
  23.             if (bugIndex >= 0 && bugIndex < sizeField)
  24.             {
  25.                 if (field[bugIndex] == 1) //Check if there is an actual bug on that index
  26.                 {
  27.                     //Take off and leave index empty => 0
  28.                     field[bugIndex] = 0;
  29.  
  30.                     //land LEFT                
  31.                     if (flightDirection == "left")
  32.                     {
  33.                         field = NewFieldLeft(field, bugIndex, flightLength);
  34.                     }// END OF LEFT direction ---------------------------------
  35.  
  36.  
  37.                     //land RIGHT  
  38.                     if (flightDirection == "right")
  39.                     {
  40.                         field = NewFieldRight(field, bugIndex, flightLength);
  41.                     }// END OF RIGHT direction -------------------------------
  42.  
  43.  
  44.                 }//END OF Check if there is an actual bug on that index
  45.             } //END OF Check for misleading input index
  46.            
  47.             commandInput = Console.ReadLine().Split();
  48.  
  49.         } // END OF while (commandInput[0] != "end")
  50.  
  51.         // Print output
  52.         Console.WriteLine(string.Join(" ", field));
  53.     }
  54.  
  55.  
  56.  
  57.     // ------------------------- METHODS ------------------------ //
  58.  
  59.     //LEFT LAND direction / land if index is 0
  60.     static int[] NewFieldLeft(int[] field, int bugIndex, int flightLength)
  61.     {
  62.         flightLength = Math.Abs(flightLength);
  63.         for (int i = bugIndex - flightLength; i >= 0; i = i - flightLength)
  64.         {
  65.             if (field[i] == 0)
  66.             {
  67.                 field[i] = 1;
  68.                 break;
  69.             }
  70.         }
  71.         return field;
  72.     } // end of LEFT LAND ----------------------------
  73.  
  74.     //RIGHT land
  75.     private static int[] NewFieldRight(int[] field, int bugIndex, int flightLength)
  76.     {
  77.         flightLength = Math.Abs(flightLength);
  78.         for (int i = bugIndex + flightLength; i < field.Length; i = i + flightLength)
  79.         {
  80.             if (field[i] == 0)
  81.             {
  82.                 field[i] = 1;
  83.                 break;
  84.             }
  85.         }
  86.  
  87.         return field;
  88.     }// end of RIGHT LAND ----------------------------
  89.  
  90.      
  91.     //Correct flight direction (left/right) based on positive or negative flight lenght
  92.     static string FlightDirectionCorrection(string flightDirection, int flightLength)
  93.     {
  94.         string correctedDirection = flightDirection;
  95.         if (flightLength < 0)
  96.         {
  97.             switch (flightDirection)
  98.             {
  99.                 case "left": correctedDirection = "right"; break;
  100.                 case "right": correctedDirection = "left"; break;
  101.             }
  102.         }
  103.         return correctedDirection;
  104.     }
  105.     // END OF Correct flight ------------------------------
  106.  
  107.  
  108.     // Plant bugs on field - the very first task
  109.     static int[] PlantBugsOnField(int sizeField, int[] indexToPlantBugs)
  110.     {
  111.         int[] field = new int[sizeField];
  112.         for (int i = 0; i < indexToPlantBugs.Length; i++)
  113.         {
  114.             if (indexToPlantBugs[i] >= 0 && indexToPlantBugs[i] < sizeField)
  115.             {
  116.                 field[indexToPlantBugs[i]] = 1;
  117.             }
  118.         }
  119.         return field;
  120.     }
  121.     // END OF Plant bugs ------------------------------
  122. }
Add Comment
Please, Sign In to add comment