Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _10._LadyBugs
- {
- class Program
- {
- static void Main(string[] args)
- {
- int size = int.Parse(Console.ReadLine());
- int[] bugsIndexes = Console.ReadLine()
- .Split()
- .Select(int.Parse)
- .ToArray();
- bool[] field = MakeField(size, bugsIndexes);
- string input = Console.ReadLine();
- while (input != "end")
- {
- string[] tokens = input.Split();
- field = ChangePositons(field, tokens);
- input = Console.ReadLine();
- }
- for (int i = 0; i < field.Length; i++)
- {
- if (field[i] == true)
- {
- Console.Write($"{1} ");
- }
- else
- Console.Write($"{0} ");
- }
- }
- private static bool[] ChangePositons(bool[] field, string[] tokens)
- {
- int bugIndex = int.Parse(tokens[0]);
- string direction = tokens[1];
- int positionsMove = int.Parse(tokens[2]);
- if (direction == "left")
- {
- positionsMove *= -1; //choose direction
- }
- bool isIndexInField = bugIndex >= 0 && bugIndex < field.Length;
- if (isIndexInField)
- {
- int newBugIndex = bugIndex + positionsMove;
- field = moveBug(field, newBugIndex, bugIndex, positionsMove);
- }
- return field;
- }
- private static bool[] moveBug(bool[] field, int newBugIndex, int bugIndex, int positionsMove)
- {
- bool isInField = newBugIndex >= 0 && newBugIndex < field.Length;
- if (isInField)
- {
- if (field[newBugIndex] == true)
- {
- newBugIndex += positionsMove;
- moveBug(field, newBugIndex, bugIndex, positionsMove);
- }
- else
- {
- field[bugIndex] = false;
- field[newBugIndex] = true;
- }
- }
- else
- {
- field[bugIndex] = false;
- }
- return field;
- }
- private static bool[] MakeField(int size, int[] bugsIndexes)
- {
- bool[] field = new bool[size];
- for (int i = 0; i < bugsIndexes.Length; i++)
- {
- if (bugsIndexes[i] >= 0 && bugsIndexes[i] < field.Length)
- {
- field[bugsIndexes[i]] = true;
- }
- }
- return field;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement