Advertisement
fk4m1913

Untitled

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