Advertisement
Guest User

Ladybugs

a guest
Jun 25th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace LadyBugs
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] fieldSize = new int[int.Parse(Console.ReadLine())];
  12.             int[] ladyBugsIndexes = Console.ReadLine().Split().Select(int.Parse).ToArray();
  13.             List<string> commands = Console.ReadLine().Split().ToList();
  14.  
  15.             for (int i = 0; i < ladyBugsIndexes.Length; i++)
  16.             {
  17.                 fieldSize[ladyBugsIndexes[i]] = 1;
  18.             }
  19.            
  20.             while (commands[0] != "end")
  21.             {
  22.                 int startIndex = int.Parse(commands[0]);
  23.                 int flyLength = int.Parse(commands[2]);
  24.                 int endIndex = 0;
  25.  
  26.                 if (startIndex <= fieldSize.Length-1 && fieldSize[startIndex]==1) //we have a ladybug found in the field
  27.                 {
  28.                     if (commands[1]=="right")
  29.                     {
  30.                         endIndex = startIndex + flyLength;
  31.                     }
  32.                     else if (commands[1]=="left")
  33.                     {
  34.                         endIndex = startIndex - flyLength;
  35.                     }
  36.  
  37.                     if (endIndex <= fieldSize.Length-1 && endIndex >=0 && fieldSize[endIndex] !=1)
  38.                     {
  39.                         fieldSize[endIndex] = 1;
  40.                     }
  41.                     else
  42.                     {
  43.                         try
  44.                         {
  45.                             while (endIndex <= fieldSize.Length - 1 && endIndex >= 0 && fieldSize[endIndex] != 0)
  46.                             {
  47.                                 endIndex += flyLength;
  48.                             }
  49.                             fieldSize[endIndex] = 1;
  50.                         }
  51.                         catch (Exception)
  52.                         {
  53.                             fieldSize[startIndex] = 0;
  54.                             commands = Console.ReadLine().Split(" ").ToList();
  55.                             continue;
  56.                         }
  57.                     }
  58.                     fieldSize[startIndex] = 0;
  59.                 }
  60.                 else
  61.                 {
  62.                     continue;
  63.                 }
  64.                 commands = Console.ReadLine().Split(" ").ToList();
  65.             }
  66.             Console.WriteLine(string.Join(" ", fieldSize));
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement