Advertisement
YavorGrancharov

02. Ladybugs

Nov 3rd, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 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[] field = new int[int.Parse(Console.ReadLine())];
  12.             int[] bugs = Console.ReadLine().Split().Select(int.Parse).ToArray();
  13.  
  14.             for (int i = 0; i < field.Length; i++)
  15.             {
  16.                 if (bugs.Contains(i))
  17.                 {
  18.                     field[i] = 1;
  19.                 }
  20.                 else
  21.                 {
  22.                     field[i] = 0;
  23.                 }
  24.             }
  25.  
  26.             string input = Console.ReadLine();
  27.  
  28.             while (input != "end")
  29.             {
  30.                 string[] indexCommands = input.Split(' ').ToArray();          
  31.                 int ladybugIndex = int.Parse(indexCommands[0]);
  32.                 string direction = indexCommands[1];
  33.                 int flyLength = int.Parse(indexCommands[2]);
  34.            
  35.                 if (ladybugIndex < 0 || ladybugIndex >= field.Length)
  36.                 {
  37.                     input = Console.ReadLine();
  38.                     continue;
  39.                 }
  40.                 if (field[ladybugIndex] != 1)
  41.                 {
  42.                     input = Console.ReadLine();
  43.                     continue;
  44.                 }
  45.                 if (field[ladybugIndex] == 0)
  46.                 {
  47.                     input = Console.ReadLine();
  48.                     continue;
  49.                 }
  50.                 field[ladybugIndex] = 0;
  51.  
  52.                 int pos = ladybugIndex;
  53.  
  54.                 while (true)
  55.                 {
  56.                     if (direction == "right")
  57.                     {
  58.                         pos += flyLength;
  59.                     }
  60.                     else if (direction == "left")
  61.                     {
  62.                         pos -= flyLength;
  63.                     }
  64.                     if (pos < 0 || pos >= field.Length)
  65.                     {
  66.                         break;
  67.                     }
  68.                     if (field[pos] == 1)
  69.                     {
  70.                         continue;
  71.                     }
  72.                     else
  73.                     {
  74.                         field[pos] = 1;
  75.                         break;
  76.                     }
  77.                 }
  78.                 input = Console.ReadLine();
  79.             }
  80.             Console.WriteLine(string.Join(" ",field));
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement