nsavov

Untitled

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