Advertisement
bullit3189

Ladybugs - Arrays

Jan 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. public class Program
  5. {
  6. public static void Main()
  7. {
  8. int fieldSize = int.Parse(Console.ReadLine());
  9. int [] field = new int [fieldSize];
  10.  
  11. int [] initialIndexes = Console.ReadLine().Split().Select(int.Parse).ToArray();
  12.  
  13. for (int i=0; i<initialIndexes.Length; i++)
  14. {
  15. if (initialIndexes[i]>=0 && initialIndexes[i]<field.Length)
  16. {
  17. field[initialIndexes[i]] =1;
  18. }
  19. }
  20.  
  21. while (true)
  22. {
  23. string input = Console.ReadLine();
  24.  
  25. if (input == "end")
  26. {
  27. break;
  28. }
  29.  
  30. string[] command = input.Split();
  31. int ladybugIndex = int.Parse(command[0]);
  32. bool isFirst = true;
  33.  
  34. while (ladybugIndex>=0 && ladybugIndex<field.Length && field[ladybugIndex]!=0)
  35. {
  36. if (isFirst)
  37. {
  38. field[ladybugIndex]=0;
  39. isFirst = false;
  40. }
  41.  
  42. string direction = command[1];
  43. int flyLength = int.Parse(command[2]);
  44.  
  45. if (direction == "left")
  46. {
  47. ladybugIndex -= flyLength;
  48.  
  49. if (ladybugIndex<field.Length && ladybugIndex>=0)
  50. {
  51.  
  52. if (field[ladybugIndex]!=1)
  53. {
  54. field[ladybugIndex]=1;
  55. break;
  56. }
  57.  
  58. }
  59. }
  60. else
  61. {
  62. ladybugIndex += flyLength;
  63.  
  64. if (ladybugIndex<field.Length && ladybugIndex>=0)
  65. {
  66.  
  67. if (field[ladybugIndex]!=1)
  68. {
  69. field[ladybugIndex]=1;
  70. break;
  71. }
  72.  
  73. }
  74.  
  75. }
  76. }
  77.  
  78. }
  79. Console.WriteLine(string.Join(" ",field));
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement