nikiman8888

LadyBugsFixed-exam

Sep 27th, 2018
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.75 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace _06.LadyBugs
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int fieldSize = int.Parse(Console.ReadLine());
  14.  
  15.             int[] field = new int[fieldSize];
  16.  
  17.             int[] ladyBugsIndexes = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  18.  
  19.             ladyBugsIndexes = ladyBugsIndexes.Where(x => x >= 0 && x <= field.Length - 1).ToArray();
  20.  
  21.             field = PutLadyBugs(ladyBugsIndexes, field); // put ladybugs int the fields          
  22.  
  23.             string input = Console.ReadLine();
  24.  
  25.             while (input != "end")
  26.             {
  27.                 string[] tokens = input.Split(' ');
  28.  
  29.                 string direction = tokens[1];
  30.  
  31.                 int startIndex = int.Parse(tokens[0]);
  32.  
  33.                 int steps = int.Parse(tokens[2]);
  34.                
  35.                 if(startIndex >= 0 && startIndex <= field.Length - 1)
  36.                 {
  37.                     if (field[startIndex] == 1)
  38.                     {
  39.                         switch (direction)
  40.                         {
  41.                             case "right":
  42.                                 if (steps < 0)
  43.                                 {
  44.                                     steps = Math.Abs(steps); //if is minus we change direction
  45.  
  46.                                     field = GetLeftDirection(startIndex, steps, field);
  47.  
  48.                                     input = Console.ReadLine();
  49.  
  50.                                     continue;
  51.                                 }                                
  52.                                 field = GetRightDirection(startIndex, steps, field);
  53.                                 break;
  54.                             case "left":
  55.  
  56.                                 if (steps < 0)
  57.                                 {
  58.                                     steps = Math.Abs(steps); //if is minus we change direction
  59.  
  60.                                     field = GetRightDirection(startIndex, steps, field);
  61.  
  62.                                     input = Console.ReadLine();
  63.  
  64.                                     continue;
  65.  
  66.                                 }
  67.                                 field = GetLeftDirection(startIndex, steps, field);
  68.                                 break;                          
  69.                         }
  70.                     }
  71.                 }                                    
  72.                 input = Console.ReadLine();
  73.             }
  74.             for (int i = 0; i < field.Length; i++)
  75.             {
  76.                 if(field[i] != 1)
  77.                 {
  78.                     field[i] = 0;
  79.                 }
  80.             }
  81.             Console.WriteLine(string.Join(" ",field));
  82.         }
  83.         private static int[] GetLeftDirection(int startIndex, int steps, int[] field)
  84.         {
  85.             long next = startIndex - steps; //get next index before start to Fly
  86.  
  87.             field[startIndex] = 0; // start to fly end empty the index
  88.  
  89.             while (true)
  90.             {
  91.                 if (next >= 0)
  92.                 {
  93.                     if (field[next] == 1)
  94.                     {                        
  95.                         next -= steps;
  96.                     }
  97.                     else
  98.                     {
  99.                         field[next] = 1;
  100.                         break;
  101.                     }
  102.                 }
  103.                 else
  104.                 {
  105.                     break;
  106.                 }
  107.             }
  108.             return field;
  109.         }
  110.         private static int[] GetRightDirection(int startIndex, int steps, int[] field)
  111.         {
  112.             long next = startIndex + steps; //get next index before start to Fly
  113.  
  114.             field[startIndex] = 0; // start to fly end empty the index
  115.  
  116.             while (true)
  117.             {              
  118.                 if (next <= field.Length -1)
  119.                 {
  120.                     if (field[next] == 1)
  121.                     {                    
  122.                         next += steps;
  123.                     }
  124.                     else
  125.                     {
  126.                         field[next] = 1;
  127.  
  128.                         break;
  129.                     }
  130.                 }
  131.                 else
  132.                 {
  133.                     break;
  134.                 }
  135.             }
  136.  
  137.             return field;
  138.         }
  139.         private static int[] PutLadyBugs(int[] ladyBugsIndexes, int[] field)
  140.         {
  141.             for (int i = 0; i < ladyBugsIndexes.Length; i++)
  142.             {
  143.                 int currentLaybug = ladyBugsIndexes[i];
  144.  
  145.                 field[currentLaybug] = 1;
  146.             }
  147.             return field;
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment