Advertisement
yanass

LadyBugs

Jun 5th, 2019
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Ladybugs_second_try
  5. {
  6.     class Program
  7.     {
  8.         static string MoveDir(string moveDirection, int lengthMove)
  9.         {
  10.             if (moveDirection == "right" && lengthMove < 0)
  11.                 moveDirection = "left";
  12.            
  13.             else if (moveDirection == "left" && lengthMove < 0)
  14.                 moveDirection = "right";
  15.            
  16.             return moveDirection;
  17.         }
  18.         static void Main()
  19.         {
  20.             int fieldSize = int.Parse(Console.ReadLine());
  21.  
  22.             //create the field
  23.             int[] field = new int[fieldSize];
  24.  
  25.             //enter the initial positions
  26.             int[] initialPos = Console.ReadLine().Split().Select(int.Parse).ToArray();
  27.  
  28.             //put the bugs in place
  29.  
  30.             for (int i = 0; i < initialPos.Length; i++)
  31.             {
  32.                 if (initialPos[i] >= 0 && initialPos[i] < field.Length)
  33.                 {
  34.                     field[initialPos[i]] = 1;
  35.  
  36.                 }
  37.             }
  38.  
  39.             //first command
  40.             string commandMove = Console.ReadLine();
  41.  
  42.             //set the process for commands
  43.  
  44.             while (commandMove != "end")
  45.             {
  46.                 string[] directions = commandMove.Split();
  47.                 int currPos = int.Parse(directions[0]);
  48.                 string moveDirection = directions[1];
  49.                 int lenghtMove = int.Parse(directions[2]);
  50.  
  51.                 if (lenghtMove == 0)
  52.                 {                    
  53.                     break;                    
  54.                 }
  55.  
  56.                 else if (currPos >= 0 && currPos < field.Length && field[currPos] != 0)
  57.                 {
  58.                     int newPos = currPos;
  59.  
  60.                     moveDirection = MoveDir(moveDirection, lenghtMove);
  61.                     lenghtMove = Math.Abs(lenghtMove);
  62.  
  63.                     if (moveDirection == "right")
  64.                     {
  65.                         while (newPos < field.Length && field[newPos] == 1)
  66.                         {
  67.                             newPos += lenghtMove;
  68.                         }
  69.  
  70.                     }
  71.                     else if (moveDirection == "left")
  72.                     {
  73.                         while (newPos >= 0 && field[newPos] == 1)
  74.                         {
  75.                             newPos -= lenghtMove;
  76.                         }
  77.  
  78.                     }
  79.  
  80.                     if (newPos >= 0 && newPos < field.Length)
  81.                     {
  82.                         field[newPos] = 1;
  83.  
  84.                     }
  85.                     field[currPos] = 0;
  86.                 }
  87.                                
  88.                 commandMove = Console.ReadLine();
  89.             }
  90.  
  91.             Console.WriteLine(string.Join(' ', field));
  92.         }
  93.  
  94.        
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement