Advertisement
bacco

Ladybugs

Jun 19th, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace test
  6. {
  7.     class MainClass
  8.     {
  9.         public static void Main(string[] args)
  10.  
  11.         {
  12.             var size = int.Parse(Console.ReadLine());
  13.  
  14.             var field = new int[size];
  15.  
  16.             var ladybugIndex = Console.ReadLine()
  17.                                       .Split(new[]{ ' ' }
  18.                                              ,StringSplitOptions
  19.                                              .RemoveEmptyEntries)
  20.                                       .Select(int.Parse)
  21.                                       .Where(x => x >= 0 && x < size)
  22.                                       .ToList();
  23.  
  24.             foreach (var index in ladybugIndex)
  25.             {
  26.                 field[index] = 1;
  27.             }
  28.  
  29.             while (true)
  30.             {
  31.                 var line = Console.ReadLine();
  32.  
  33.                 if (line == "end")
  34.                 {
  35.                     break;
  36.                 }
  37.  
  38.                 var commandParts = line.Split(' ');
  39.  
  40.                 var currentLadybug = int.Parse(commandParts[0]);
  41.                 var direction      = commandParts[1];
  42.                 var flyLenght      = int.Parse(commandParts[2]);
  43.  
  44.                 if (direction == "left")
  45.                 {
  46.                     flyLenght *= -1;
  47.                 }
  48.  
  49.                 if ( currentLadybug <  0  ||
  50.                      currentLadybug >= size )
  51.                 {
  52.                     continue;
  53.                 }
  54.  
  55.                 if ( field[currentLadybug] == 0 )
  56.                 {
  57.                     continue;
  58.                 }
  59.  
  60.                 field[currentLadybug] = 0;
  61.  
  62.  
  63.                 var nextIndexToLand = currentLadybug;
  64.  
  65.  
  66.                 while (true)
  67.                 {
  68.                     nextIndexToLand += flyLenght;
  69.  
  70.                     if ( nextIndexToLand <  0  ||
  71.                          nextIndexToLand >= size )
  72.                     {
  73.                         break;
  74.                     }
  75.  
  76.                     if (field[nextIndexToLand] == 1)
  77.                     {
  78.                         continue;
  79.                     }
  80.  
  81.                     field[nextIndexToLand] = 1;
  82.                     break;
  83.                 }
  84.             }
  85.  
  86.             Console.WriteLine(string.Join(" ", field));
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement