MiroslavKisov

LadyBugs

Dec 1st, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _02.Ladybugs
  5. {
  6.     public class StartUp
  7.     {
  8.         public static void Main()
  9.         {
  10.             int fieldSize = int.Parse(Console.ReadLine());
  11.             var ladyBugIndexes = Console.ReadLine()
  12.                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  13.                 .Select(int.Parse)
  14.                 .ToArray();
  15.             bool[] field = new bool[fieldSize];
  16.             for (int i = 0; i < field.Length; i++)
  17.             {
  18.                 for (int j = 0; j < ladyBugIndexes.Length; j++)
  19.                 {
  20.                     if (i == ladyBugIndexes[j])
  21.                     {
  22.                         field[i] = true;
  23.                     }
  24.                 }
  25.             }
  26.             while (true)
  27.             {
  28.                 string command = Console.ReadLine();
  29.                 if (command == "end")
  30.                 {
  31.                     break;
  32.                 }
  33.                 var commandArgs = command
  34.                     .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  35.                     .ToArray();
  36.                 int startIndex = int.Parse(commandArgs[0]);
  37.                 string direction = commandArgs[1];
  38.                 int flightDistance = int.Parse(commandArgs[2]);
  39.                 if (startIndex < 0 || startIndex > field.Length - 1
  40.                     || field[startIndex] == false || flightDistance == 0)
  41.                 {
  42.                     continue;
  43.                 }
  44.                 if (direction == "right")
  45.                 {
  46.                     if (flightDistance >= 0)
  47.                     {
  48.                         FlyRight(startIndex, field, flightDistance);
  49.                     }
  50.                     else
  51.                     {
  52.                         FlyLeft(startIndex, field, flightDistance);
  53.                     }
  54.                 }
  55.                 else if (direction == "left")
  56.                 {
  57.                     if (flightDistance >= 0)
  58.                     {
  59.                         FlyLeft(startIndex, field, flightDistance);
  60.                     }
  61.                     else
  62.                     {
  63.                         FlyRight(startIndex, field, flightDistance);
  64.                     }
  65.                 }
  66.             }
  67.             PrintResult(field);
  68.         }
  69.  
  70.         private static void PrintResult(bool[] field)
  71.         {
  72.             for (int i = 0; i < field.Length; i++)
  73.             {
  74.                 if (field[i] == true)
  75.                 {
  76.                     Console.Write(1 + " ");
  77.                 }
  78.                 else
  79.                 {
  80.                     Console.Write(0 + " ");
  81.                 }
  82.             }
  83.         }
  84.  
  85.         private static void FlyLeft(int startIndex, bool[] field, int flightDistance)
  86.         {
  87.             bool isBugLanded = false;
  88.             flightDistance = Math.Abs(flightDistance);
  89.             for (int i = startIndex; i >= 0; i -= flightDistance)
  90.             {
  91.                 if (field[i] == false)
  92.                 {
  93.                     field[i] = true;
  94.                     isBugLanded = true;
  95.                 }
  96.                 field[startIndex] = false;
  97.                 if (isBugLanded)
  98.                 {
  99.                     return;
  100.                 }
  101.             }
  102.         }
  103.  
  104.         private static void FlyRight(int startIndex, bool[] field, int flightDistance)
  105.         {
  106.             bool isBugLanded = false;
  107.             flightDistance = Math.Abs(flightDistance);
  108.             for (int i = startIndex; i <= field.Length - 1; i += flightDistance)
  109.             {
  110.                 if (field[i] == false)
  111.                 {
  112.                     field[i] = true;
  113.                     isBugLanded = true;
  114.                 }
  115.                 field[startIndex] = false;
  116.                 if (isBugLanded)
  117.                 {
  118.                     return;
  119.                 }
  120.             }
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment