Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Ladybugs
- {
- class Program
- {
- private static int[] allLadyBugs;
- static void Main()
- {
- int fieldSize = int.Parse(Console.ReadLine());
- var ladyBugsIndexes = Console.ReadLine()
- .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .OrderBy(s => s)
- .ToArray();
- allLadyBugs = new int[fieldSize];
- foreach (var index in ladyBugsIndexes)
- {
- if (index >= 0 && index < fieldSize)
- {
- allLadyBugs[index]++;
- }
- }
- string input = Console.ReadLine();
- while (input != "end")
- {
- var splitted = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- int direction = splitted[1] == "right" ? 1 : -1;
- if (!valid(int.Parse(splitted[0]), int.Parse(splitted[2]), direction))
- {
- input = Console.ReadLine();
- continue;
- }
- MoveLadyBird(int.Parse(splitted[0]), int.Parse(splitted[2]), direction);
- input = Console.ReadLine();
- }
- Console.WriteLine(string.Join(" ", allLadyBugs));
- }
- private static void MoveLadyBird(int source, int flyLength, int direction)
- {
- int index = source + direction * flyLength;
- if (index < 0 || index >= allLadyBugs.Length)
- {
- allLadyBugs[source]--;
- return;
- }
- if (allLadyBugs[index] == 0)
- {
- allLadyBugs[index]++;
- allLadyBugs[source]--;
- return;
- }
- allLadyBugs[index]++;
- allLadyBugs[source]--;
- MoveLadyBird(index, flyLength, direction);
- }
- private static bool valid(int source, int flyLength, int direction)
- {
- int index = source + direction * flyLength;
- if (source < 0
- || source >= allLadyBugs.Length
- || allLadyBugs[source] == 0)
- {
- return false;
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement