dremadred

Ladybugs

Feb 24th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _2.Ladybugs
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             var fieldSize = int.Parse(Console.ReadLine());
  11.             var ladybugIndexes = Console.ReadLine()
  12.                 .Split().Select(int.Parse)
  13.                 .Where(a => a >= 0 && a < fieldSize)
  14.                 .ToArray();
  15.  
  16.             var ladybugs = new int[fieldSize];
  17.  
  18.             for (int i = 0; i < ladybugIndexes.Length; i++)
  19.             {
  20.                 var currentLadyBugsIndex = ladybugIndexes[i];
  21.                 ladybugs[currentLadyBugsIndex] = 1;
  22.             }
  23.  
  24.             var line = Console.ReadLine();
  25.             while (line != "and")
  26.             {
  27.                 var tokens = line.Split();
  28.  
  29.                 var ladybugsIndex = int.Parse(tokens[0]);
  30.                 var direction = (tokens[1]);
  31.                 var flyLenght = int.Parse(tokens[2]);
  32.  
  33.                 if (ladybugsIndex < 0 || ladybugsIndex >= ladybugs.Length)
  34.                 {
  35.                     line = Console.ReadLine();
  36.                     continue;
  37.                 }
  38.                 if (ladybugs[ladybugsIndex] ==0)
  39.                 {
  40.                     line = Console.ReadLine();
  41.                     continue;
  42.                 }
  43.  
  44.                 MoveLadybug (ladybugs, ladybugsIndex, flyLenght, direction);
  45.                 line = Console.ReadLine();
  46.  
  47.             }
  48.  
  49.             Console.WriteLine(string.Join(" ", ladybugs));
  50.         }
  51.  
  52.          static void MoveLadybug(int[] ladybugs, int ladybugsIndex, int flyLenght, string direction)
  53.         {
  54.             ladybugs[ladybugsIndex] = 0;
  55.  
  56.             var leftArrayOrFoundPlase = false;
  57.             while (!leftArrayOrFoundPlase)
  58.             {
  59.                 switch (direction)
  60.                 {
  61.                     case "left":
  62.                         ladybugsIndex -= flyLenght;
  63.                         break;
  64.                     case "right":
  65.                         ladybugsIndex += flyLenght;
  66.                         break;
  67.                 }
  68.                 if (ladybugsIndex < 0 || ladybugsIndex >= ladybugs.Length)
  69.                 {
  70.                     leftArrayOrFoundPlase = true;
  71.                     continue;
  72.                 }
  73.                 if (ladybugs[ladybugsIndex]==1)
  74.                 {
  75.                     continue;
  76.                 }
  77.                 if (ladybugs[ladybugsIndex]==0)
  78.                 {
  79.                     ladybugs[ladybugsIndex] = 1;
  80.                     leftArrayOrFoundPlase = true;
  81.                     continue;
  82.                 }
  83.             }
  84.  
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment