Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.48 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks.Sources;
  4.  
  5. namespace Ladybugs
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int fieldSize = int.Parse(Console.ReadLine());
  12.             int[] bugsIndexes = Console.ReadLine().Split().Select(int.Parse).ToArray();
  13.  
  14.             int[] field = new int[fieldSize];
  15.  
  16.             if (fieldSize == 0)
  17.             {
  18.                 return;
  19.             }
  20.  
  21.             for (int i = 0; i < bugsIndexes.Length; i++)
  22.             {
  23.                 int currentIndex = bugsIndexes[i];
  24.  
  25.                 if (0 <= currentIndex && currentIndex < field.Length)
  26.                 {
  27.                     field[currentIndex] = 1;
  28.                 }
  29.             }
  30.  
  31.             while (true)
  32.             {
  33.                 string[] input = Console.ReadLine().Split().ToArray();
  34.  
  35.                 if (input[0] == "end")
  36.                 {
  37.                     break;
  38.                 }
  39.  
  40.                 int startIndex = int.Parse(input[0]);
  41.                 string direction = input[1];
  42.                 int flyLength = int.Parse(input[2]);
  43.  
  44.                 int currentIndex = startIndex;
  45.  
  46.                 if (0 <= startIndex && startIndex < field.Length)
  47.                 {
  48.                     if (flyLength < 0)
  49.                     {
  50.                         flyLength = Math.Abs(flyLength);
  51.  
  52.                         if (direction == "left")
  53.                         {
  54.                             direction = "right";
  55.                         }
  56.                         else
  57.                         {
  58.                             direction = "left";
  59.                         }
  60.                     }
  61.  
  62.                     while (true)
  63.                     {
  64.                         if (direction == "right")
  65.                         {
  66.                             int indexAfterFly = currentIndex + flyLength;
  67.  
  68.                             if (indexAfterFly >= field.Length)
  69.                             {
  70.                                 field[startIndex] = 0;
  71.                                 break;
  72.                             }
  73.  
  74.                             if (field[indexAfterFly] == 0)
  75.                             {
  76.                                 field[indexAfterFly] = 1;
  77.                                 field[startIndex] = 0;
  78.                                 break;
  79.                             }
  80.                             else
  81.                             {
  82.                                 currentIndex = indexAfterFly;
  83.                             }
  84.                         }
  85.                         else
  86.                         {
  87.                             int indexAfterFly = currentIndex - flyLength;
  88.  
  89.                             if (indexAfterFly < 0)
  90.                             {
  91.                                 field[startIndex] = 0;
  92.                                 break;
  93.                             }
  94.  
  95.                             if (field[indexAfterFly] == 0)
  96.                             {
  97.                                 field[indexAfterFly] = 1;
  98.                                 field[startIndex] = 0;
  99.                                 break;
  100.                             }
  101.                             else
  102.                             {
  103.                                 currentIndex = indexAfterFly;
  104.                             }
  105.                         }
  106.                     }
  107.                 }
  108.             }
  109.  
  110.             Console.WriteLine(String.Join(" ", field));
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement