Advertisement
yanass

LadyBugs

Jun 5th, 2019
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 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 (currPos >= 0 && currPos < field.Length && field[currPos] != 0)
  52.                 {
  53.                     int newPos = currPos;
  54.  
  55.                     moveDirection = MoveDir(moveDirection, lenghtMove);
  56.                     lenghtMove = Math.Abs(lenghtMove);
  57.  
  58.                     if (moveDirection == "right")
  59.                     {
  60.                         while (newPos < field.Length && field[newPos] == 1)
  61.                         {
  62.                             newPos += lenghtMove;
  63.                         }
  64.  
  65.                     }
  66.                     else if (moveDirection == "left")
  67.                     {
  68.                         while (newPos >= 0 && field[newPos] == 1)
  69.                         {
  70.                             newPos -= lenghtMove;
  71.                         }
  72.  
  73.                     }
  74.  
  75.                     if (newPos >= 0 && newPos < field.Length)
  76.                     {
  77.                         field[newPos] = 1;
  78.  
  79.                     }
  80.                     field[currPos] = 0;
  81.                 }
  82.                 commandMove = Console.ReadLine();
  83.             }
  84.  
  85.             Console.WriteLine(string.Join(' ', field));
  86.         }
  87.  
  88.        
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement