simeon3000

Ladybugs

Sep 8th, 2017
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1.             int n = int.Parse(Console.ReadLine());
  2.             int[] indexes = Console.ReadLine().Split().Select(int.Parse).ToArray();
  3.  
  4.             int[] ladybugs = new int[n];
  5.  
  6.             foreach (int index in indexes.Where(x => x >= 0 && x < n))
  7.             {
  8.                 ladybugs[index] = 1;
  9.             }
  10.  
  11.             string input = Console.ReadLine();
  12.             while (input != "end")
  13.             {
  14.                 string[] myArr = input.Split();
  15.                 int startIndex = int.Parse(myArr[0]);
  16.                 string direction = myArr[1];
  17.                 int steps = int.Parse(myArr[2]);
  18.  
  19.                 if (startIndex >= 0 && startIndex < n && ladybugs[startIndex] == 1)
  20.                 {
  21.                     ladybugs[startIndex] = 0;
  22.  
  23.                     try
  24.                     {
  25.                         switch (direction)
  26.                         {
  27.                             case "left":
  28.                                 while (ladybugs[startIndex - steps] == 1)
  29.                                 {
  30.                                     startIndex -= steps;
  31.                                 }
  32.                                 ladybugs[startIndex - steps] = 1;
  33.                                 break;
  34.                                
  35.                             case "right":
  36.                                 while (ladybugs[startIndex + steps] == 1)
  37.                                 {
  38.                                     startIndex += steps;
  39.                                 }
  40.                                 ladybugs[startIndex + steps] = 1;
  41.                                 break;
  42.                         }
  43.                     }
  44.                     catch (Exception)
  45.                     {
  46.                     }                  
  47.                 }
  48.                 input = Console.ReadLine();
  49.             }
  50.             Console.WriteLine(string.Join(" ", ladybugs));
Advertisement
Add Comment
Please, Sign In to add comment