Advertisement
WindFell

LadyBugs

Mar 18th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class Ladybugs
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         int fieldSize = int.Parse(Console.ReadLine());
  9.         int[] field = new int[fieldSize];
  10.         Console.ReadLine()
  11.             .Split()
  12.             .Select(int.Parse)
  13.             .Where(i => i >= 0 && i < fieldSize)
  14.             .ToList()
  15.             .ForEach(i => field[i] = 1);
  16.         string command = Console.ReadLine();
  17.  
  18.         while (command != "end")
  19.         {
  20.             string[] tokens = command
  21.                 .Split()
  22.                 .ToArray();
  23.  
  24.             int ladybugIndex = int.Parse(tokens[0]);
  25.             string direction = tokens[1];
  26.             int flyLength = int.Parse(tokens[2]);
  27.             int landIndex = ladybugIndex;
  28.  
  29.             if (direction == "left")
  30.             {
  31.                 flyLength *= -1;
  32.             }
  33.  
  34.             if (ladybugIndex < 0 || ladybugIndex >= fieldSize || field[ladybugIndex] == 0)
  35.             {
  36.                 continue;
  37.             }
  38.  
  39.             field[ladybugIndex] = 0;
  40.  
  41.             while (true)
  42.             {
  43.                 landIndex += flyLength;
  44.  
  45.                 if (landIndex < 0 || landIndex >= fieldSize)
  46.                 {
  47.                     break;
  48.                 }
  49.  
  50.                 if (field[landIndex] == 0)
  51.                 {
  52.                     field[landIndex] = 1;
  53.                     break;
  54.                 }
  55.             }
  56.  
  57.             command = Console.ReadLine();
  58.         }
  59.  
  60.         Console.WriteLine(string.Join(" ", field));
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement