Advertisement
Guest User

Untitled

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