Advertisement
miroLLL

02Problem-Ladybugs

Mar 1st, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02Problem_Ladybugs
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int fieldSize = int.Parse(Console.ReadLine());
  12.             long[] flyField = new long[fieldSize];
  13.             string flyCommand = string.Empty;
  14.  
  15.             long[] buggedIndexes = Console.ReadLine().Split().Select(long.Parse).ToArray();
  16.  
  17.             foreach (var buggedIndex in buggedIndexes)
  18.             {
  19.                 if (buggedIndex >= 0 && buggedIndex <= flyField.Length - 1)
  20.                 {
  21.                     flyField[buggedIndex] = 1;
  22.                 }
  23.             }
  24.  
  25.             while ((flyCommand = Console.ReadLine()) != "end")
  26.             {
  27.                 string[] tokens = flyCommand.Split().Select(x => x.Trim()).ToArray();
  28.  
  29.                 long startIndex = long.Parse(tokens[0]);
  30.                 long flyLength = long.Parse(tokens[2]);
  31.                 bool isOutside = startIndex < 0 || startIndex > flyField.Length - 1;
  32.  
  33.                 if (isOutside || flyLength == 0)
  34.                 {
  35.                     continue;
  36.                 }
  37.  
  38.                 string direction = tokens[1];
  39.                 long landed = startIndex;
  40.  
  41.                 while (flyField[startIndex] == 1)
  42.                 {
  43.                     switch (direction)
  44.                     {
  45.                         case "right":
  46.                             landed += flyLength;
  47.                             break;
  48.  
  49.                         case "left":
  50.                             landed -= flyLength;
  51.                             break;
  52.                     }
  53.  
  54.                     if (landed > flyField.Length - 1 || landed < 0)
  55.                     {
  56.                         flyField[startIndex] = 0;
  57.                         break;
  58.                     }
  59.  
  60.                     if (flyField[landed] == 1)
  61.                     {
  62.                         continue;
  63.                     }
  64.  
  65.                     flyField[startIndex] = 0;
  66.                     flyField[landed] = 1;
  67.                     break;
  68.                 }
  69.             }
  70.  
  71.             Console.WriteLine(string.Join(" ", flyField));
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement