Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace _06.LadyBugs
- {
- class Program
- {
- static void Main(string[] args)
- {
- int fieldSize = int.Parse(Console.ReadLine());
- int[] field = new int[fieldSize];
- int[] ladyBugsIndexes = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- ladyBugsIndexes = ladyBugsIndexes.Where(x => x >= 0 && x <= field.Length - 1).ToArray();
- field = PutLadyBugs(ladyBugsIndexes, field); // put ladybugs int the fields
- string input = Console.ReadLine();
- while (input != "end")
- {
- string[] tokens = input.Split(' ');
- string direction = tokens[1];
- int startIndex = int.Parse(tokens[0]);
- int steps = int.Parse(tokens[2]);
- if(startIndex >= 0 && startIndex <= field.Length - 1)
- {
- if (field[startIndex] == 1)
- {
- switch (direction)
- {
- case "right":
- if (steps < 0)
- {
- steps = Math.Abs(steps); //if is minus we change direction
- field = GetLeftDirection(startIndex, steps, field);
- input = Console.ReadLine();
- continue;
- }
- field = GetRightDirection(startIndex, steps, field);
- break;
- case "left":
- if (steps < 0)
- {
- steps = Math.Abs(steps); //if is minus we change direction
- field = GetRightDirection(startIndex, steps, field);
- input = Console.ReadLine();
- continue;
- }
- field = GetLeftDirection(startIndex, steps, field);
- break;
- }
- }
- }
- input = Console.ReadLine();
- }
- for (int i = 0; i < field.Length; i++)
- {
- if(field[i] != 1)
- {
- field[i] = 0;
- }
- }
- Console.WriteLine(string.Join(" ",field));
- }
- private static int[] GetLeftDirection(int startIndex, int steps, int[] field)
- {
- long next = startIndex - steps; //get next index before start to Fly
- field[startIndex] = 0; // start to fly end empty the index
- while (true)
- {
- if (next >= 0)
- {
- if (field[next] == 1)
- {
- startIndex = (int)next;
- next -= steps;
- }
- else
- {
- field[next] = 1;
- break;
- }
- }
- else
- {
- break;
- }
- }
- return field;
- }
- private static int[] GetRightDirection(int startIndex, int steps, int[] field)
- {
- long next = startIndex + steps; //get next index before start to Fly
- field[startIndex] = 0; // start to fly end empty the index
- while (true)
- {
- if (next <= field.Length -1)
- {
- if (field[next] == 1)
- {
- startIndex = (int)next;
- next += steps;
- }
- else
- {
- field[next] = 1;
- break;
- }
- }
- else
- {
- break;
- }
- }
- return field;
- }
- private static int[] PutLadyBugs(int[] ladyBugsIndexes, int[] field)
- {
- for (int i = 0; i < ladyBugsIndexes.Length; i++)
- {
- int currentLaybug = ladyBugsIndexes[i];
- field[currentLaybug] = 1;
- }
- return field;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment