nikiman8888

LadyBugs-ArrayIterationExam

Sep 27th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.82 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.                 }
  73.                                    
  74.                 input = Console.ReadLine();
  75.             }
  76.             for (int i = 0; i < field.Length; i++)
  77.             {
  78.                 if(field[i] != 1)
  79.                 {
  80.                     field[i] = 0;
  81.                 }
  82.             }
  83.             Console.WriteLine(string.Join(" ",field));
  84.         }
  85.         private static int[] GetLeftDirection(int startIndex, int steps, int[] field)
  86.         {
  87.             long next = startIndex - steps; //get next index before start to Fly
  88.  
  89.             field[startIndex] = 0; // start to fly end empty the index
  90.  
  91.             while (true)
  92.             {
  93.                 if (next >= 0)
  94.                 {
  95.                     if (field[next] == 1)
  96.                     {
  97.                         startIndex = (int)next;
  98.                         next -= steps;
  99.                     }
  100.                     else
  101.                     {
  102.                         field[next] = 1;
  103.  
  104.                         break;
  105.                     }
  106.                 }
  107.                 else
  108.                 {
  109.                     break;
  110.                 }
  111.             }
  112.  
  113.  
  114.             return field;
  115.         }
  116.  
  117.         private static int[] GetRightDirection(int startIndex, int steps, int[] field)
  118.         {
  119.             long next = startIndex + steps; //get next index before start to Fly
  120.  
  121.             field[startIndex] = 0; // start to fly end empty the index
  122.  
  123.             while (true)
  124.             {              
  125.                 if (next <= field.Length -1)
  126.                 {
  127.                     if (field[next] == 1)
  128.                     {
  129.                         startIndex = (int)next;
  130.  
  131.                         next += steps;
  132.                     }
  133.                     else
  134.                     {
  135.                         field[next] = 1;
  136.  
  137.                         break;
  138.                     }
  139.                 }
  140.                 else
  141.                 {
  142.                     break;
  143.                 }
  144.             }
  145.  
  146.             return field;
  147.         }
  148.         private static int[] PutLadyBugs(int[] ladyBugsIndexes, int[] field)
  149.         {
  150.             for (int i = 0; i < ladyBugsIndexes.Length; i++)
  151.             {
  152.                 int currentLaybug = ladyBugsIndexes[i];
  153.  
  154.                 field[currentLaybug] = 1;
  155.             }
  156.             return field;
  157.         }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment