Advertisement
IvanBorisovG

Exam Preparation II - 02. Ladybugs

Mar 1st, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using System.Numerics;
  6. using System.Text;
  7.  
  8.  
  9. namespace _02.Ladybugs
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             var sizeOfTheFiled = int.Parse(Console.ReadLine());
  16.            
  17.             var field = new int[sizeOfTheFiled];
  18.            
  19.             var ladybugsIndexes = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  20.                 .Select(int.Parse)
  21.                 .Where(i => i >= 0 && i < sizeOfTheFiled) // Only these between 0 and sizeOfTheFiled !
  22.                 .ToList();
  23.             // fill the arr(filed) -> (If sizeOfTheFiled = 3 and ladybugsIndexes = 0 1 -> field[0] = 1 and filed[1] = 1; -> ! Debug !
  24.             foreach (var index in ladybugsIndexes)
  25.             {
  26.                 field[index] = 1;
  27.             }
  28.  
  29.             while (true)
  30.             {
  31.                 var command = Console.ReadLine().Split(' ');
  32.  
  33.                 if (command[0] == "end")
  34.                 {
  35.                     break;
  36.                 }
  37.  
  38.                 var currentLadybugIndex = int.Parse(command[0]);
  39.                 var direction = command[1];
  40.                 var flyLength = int.Parse(command[2]);
  41.                 // Ако е "left" обръщаме знака !
  42.                 if (direction == "left")
  43.                 {
  44.                     flyLength *= -1;
  45.                 }
  46.                 // outside of the field !
  47.                 if (currentLadybugIndex < 0 || currentLadybugIndex >= sizeOfTheFiled)
  48.                 {
  49.                     continue;
  50.                 }
  51.                 //there is no ladybug in this cell !
  52.                 if (field[currentLadybugIndex] == 0)
  53.                 {
  54.                     continue;
  55.                 }
  56.                 // Fly away
  57.                 field[currentLadybugIndex] = 0;
  58.                 // Set the nextIndex to land !
  59.                 var nextIndexToLand = currentLadybugIndex;
  60.  
  61.                 while (true)
  62.                 {   // increase to the next indexToLand , if there is ladybug in the cell !
  63.                     nextIndexToLand += flyLength;
  64.                     // outside of the field !
  65.                     if (nextIndexToLand < 0 || nextIndexToLand >= sizeOfTheFiled)
  66.                     {
  67.                         break;
  68.                     }
  69.                     // there is ladybug in this cell !
  70.                     if (field[nextIndexToLand] == 1)
  71.                     {
  72.                         continue;
  73.                     }
  74.                     // If the current cell is empty(field[nextIndexToLand] == 0 ,set it to 1), land here and break !
  75.                     field[nextIndexToLand] = 1;
  76.                     break;
  77.                 }
  78.             }
  79.  
  80.             Console.WriteLine(string.Join(" ", field));
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement