Advertisement
YORDAN2347

LadyBugs

Jan 30th, 2021
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _10._LadyBugs
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int size = int.Parse(Console.ReadLine());
  11.  
  12.             int[] bugsIndexes = Console.ReadLine()
  13.                 .Split()
  14.                 .Select(int.Parse)
  15.                 .ToArray();
  16.  
  17.             bool[] field = MakeField(size, bugsIndexes);
  18.  
  19.             string input = Console.ReadLine();
  20.  
  21.             while (input != "end")
  22.             {
  23.                 string[] tokens = input.Split();
  24.                 field = ChangePositons(field, tokens);
  25.  
  26.                 input = Console.ReadLine();
  27.             }
  28.  
  29.             for (int i = 0; i < field.Length; i++)
  30.             {
  31.                 if (field[i] == true)
  32.                 {
  33.                     Console.Write($"{1} ");
  34.                 }
  35.                 else
  36.                     Console.Write($"{0} ");
  37.             }
  38.         }
  39.  
  40.         private static bool[] ChangePositons(bool[] field, string[] tokens)
  41.         {
  42.             int bugIndex = int.Parse(tokens[0]);
  43.             string direction = tokens[1];
  44.             int positionsMove = int.Parse(tokens[2]);
  45.  
  46.             if (direction == "left")
  47.             {
  48.                 positionsMove *= -1; //choose direction
  49.             }
  50.  
  51.             bool isIndexInField = bugIndex >= 0 && bugIndex < field.Length;
  52.             if (isIndexInField)
  53.             {
  54.                 int newBugIndex = bugIndex + positionsMove;
  55.                 field = moveBug(field, newBugIndex, bugIndex, positionsMove);
  56.             }
  57.  
  58.             return field;
  59.         }
  60.  
  61.         private static bool[] moveBug(bool[] field, int newBugIndex, int bugIndex, int positionsMove)
  62.         {
  63.             bool isInField = newBugIndex >= 0 && newBugIndex < field.Length;
  64.             if (isInField)
  65.             {
  66.                 if (field[newBugIndex] == true)
  67.                 {
  68.                     newBugIndex += positionsMove;
  69.                     moveBug(field, newBugIndex, bugIndex, positionsMove);
  70.                 }
  71.                 else
  72.                 {
  73.                     field[bugIndex] = false;
  74.                     field[newBugIndex] = true;
  75.                 }
  76.             }
  77.             else
  78.             {
  79.                 field[bugIndex] = false;
  80.             }
  81.            
  82.             return field;
  83.         }
  84.  
  85.         private static bool[] MakeField(int size, int[] bugsIndexes)
  86.         {
  87.             bool[] field = new bool[size];
  88.  
  89.             for (int i = 0; i < bugsIndexes.Length; i++)
  90.             {
  91.                 if (bugsIndexes[i] >= 0 && bugsIndexes[i] < field.Length)
  92.                 {
  93.                     field[bugsIndexes[i]] = true;
  94.                 }
  95.             }
  96.  
  97.             return field;
  98.         }
  99.     }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement