Advertisement
Guest User

Untitled

a guest
Jan 29th, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | Source Code | 0 0
  1.  
  2. int fieldSize = int.Parse(Console.ReadLine());
  3. int[] field = new int[fieldSize];
  4.  
  5. //Populating bugs on the field:
  6. int[] bugPlaces = Console.ReadLine().Split().Select(int.Parse).ToArray();
  7. for (int i = 0; i < bugPlaces.Length; i++)
  8.     {
  9.     int pos = bugPlaces[i];
  10.     field[pos] = 1;
  11.     }
  12.  
  13. string input;
  14.  
  15. string[] cmd = new string[3]; //Command holder: [0]-position, [1]-direction, [2]-steps
  16.  
  17. while ((input = Console.ReadLine()) != "end")
  18. {
  19.     cmd = input.Split().ToArray();
  20.  
  21.     int pos = int.Parse(cmd[0]);
  22.     int steps = int.Parse(cmd[2]);
  23.     int vector = 0;
  24.     if (cmd[1] == "right") { vector = 1; }
  25.     else if (cmd[1] == "left") { vector = -1; }
  26.  
  27.     //Give order
  28.     if (pos < 0 || pos > field.Length - 1 || field[pos] == 0)
  29.     {
  30.         continue; //Nothing happens
  31.     }
  32.     else
  33.     {
  34.         int afterFlight = pos + vector * steps;
  35.         if (afterFlight > field.Length - 1 || afterFlight < 0)
  36.         {
  37.             field[pos] = 0; //The bug flew away
  38.             continue;
  39.         }
  40.         else
  41.         {
  42.             if (field[afterFlight] == 0)
  43.             {
  44.             field[pos] = 0;
  45.             field[afterFlight] = 1;
  46.             }
  47.             else
  48.             {
  49.             //Flying until there is a place for landing:
  50.             while (field[afterFlight] == 1)
  51.                 {
  52.                     field[pos] = 0;
  53.                     pos = afterFlight;
  54.                     afterFlight = pos + vector * steps;
  55.                     if (afterFlight < 0 || afterFlight > field.Length - 1)
  56.                     {
  57.                         break;
  58.                     }
  59.                     else
  60.                     {
  61.                         field[afterFlight] = 1;
  62.                         break;
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }
  69. Console.WriteLine(String.Join(" ", field));
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement