Advertisement
ghostd0g

Untitled

Mar 2nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Exercise2
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.            
  14.             int[] field = new int[int.Parse(Console.ReadLine())];
  15.  
  16.             int[] bugs = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  17.  
  18.            
  19.  
  20.             for (int i = 0; i < bugs.Length; i++)
  21.             {
  22.                 if (bugs[i] >= 0 && bugs[i] < field.Length)
  23.                 {
  24.                     field[bugs[i]] = 1; // filling the field with bugs
  25.                 }
  26.                
  27.             }
  28.  
  29.             while (true)
  30.             {
  31.                 string[] input = Console.ReadLine().Split(new char[] {' '},StringSplitOptions.RemoveEmptyEntries);
  32.  
  33.                 if (input[0] == "end")
  34.                 {
  35.                     break;
  36.                 }
  37.  
  38.                 int currentIndex = int.Parse(input[0]);
  39.                 string direction = input[1];
  40.                 int flyLength = int.Parse(input[2]);
  41.  
  42.                 if (currentIndex < 0 || currentIndex > (field.Length - 1))
  43.                 {
  44.                     continue;  // if index of the bug is out of the array, do nothing
  45.                 }
  46.  
  47.                 if (field[currentIndex] == 0)
  48.                 {
  49.                     continue; // if there is no bug on that index, do nothing
  50.                 }
  51.  
  52.                
  53.                 field[currentIndex] = 0;  // remove the bug from current index
  54.                 while (true)           // and start checking where it is going to land
  55.                 {
  56.                     if (direction == "right")
  57.                     {
  58.                         if (currentIndex + flyLength > field.Length - 1)
  59.                         {
  60.                             break;  // it flies out of the field
  61.                         }
  62.                         else if (field[currentIndex + flyLength] == 1)
  63.                         {
  64.                             flyLength = flyLength * 2; // there is another bug on current index, fly again
  65.                         }
  66.                         else
  67.                         {
  68.                             field[currentIndex + flyLength] = 1;  // bug finally landed
  69.                             break;
  70.                         }
  71.                     }
  72.  
  73.                     if (direction == "left")
  74.                     {
  75.                         if (currentIndex - flyLength < 0)
  76.                         {
  77.                             break; // it flies out of the field
  78.                         }
  79.                         else if (field[currentIndex - flyLength] == 1)
  80.                         {
  81.                             flyLength = flyLength * 2; // there is another bug on current index, fly again
  82.                         }
  83.                         else
  84.                         {
  85.                             field[currentIndex - flyLength] = 1;  // bug finally landed
  86.                             break;
  87.                         }
  88.                     }
  89.                 }
  90.  
  91.                
  92.  
  93.             }
  94.  
  95.             Console.WriteLine(string.Join(" ", field));
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement