Advertisement
Mitax

2.Ladybugs

Apr 17th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _2.Ladybugs
  5. {
  6. class LadyBugs
  7. {
  8. static void Main(string[] args)
  9. {
  10. int[] field = new int[int.Parse(Console.ReadLine())];
  11. var ladyBugsIndexes = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  12.  
  13. foreach (var position in field)
  14. {
  15. field[position] = 0;
  16. }
  17.  
  18. for (int i = 0; i < ladyBugsIndexes.Length; i++)
  19. {
  20. if (ladyBugsIndexes[i] < field.Length && ladyBugsIndexes[i] >= 0)
  21. {
  22. field[ladyBugsIndexes[i]] = 1;
  23. }
  24. }
  25.  
  26. var command = Console.ReadLine().Split(' ').ToArray();
  27.  
  28. while (!command.Contains("end"))
  29. {
  30. var bugIndex = int.Parse(command[0]);
  31. var direction = command[1];
  32. var flyLength = int.Parse(command[2]);
  33.  
  34. if (bugIndex < field.Length)
  35. {
  36. if (field[bugIndex] == 1) //there is a lady bug inside
  37. {
  38. field = ReturnIndex(direction, bugIndex, flyLength, field);
  39. }
  40. }
  41.  
  42. command = Console.ReadLine().Split(' ').ToArray();
  43. }
  44.  
  45. foreach (var item in field)
  46. {
  47. Console.Write($"{item} ");
  48. }
  49. }
  50.  
  51. private static int[] ReturnIndex(string direction, int bugIndex, int flyLength, int[] field)
  52. {
  53. //Left
  54. if (direction == "left")
  55. {
  56. if (bugIndex - flyLength < 0) //remove lady bug
  57. {
  58. field[bugIndex] = 0;
  59. }
  60. else
  61. {
  62. var positionAfterMove = bugIndex - flyLength;
  63. bool trigger = false;
  64.  
  65. while (trigger == false)
  66. {
  67. if (positionAfterMove < 0)
  68. {
  69. field[bugIndex] = 0;
  70. break;
  71. }
  72. if (field[positionAfterMove] == 0) //empty spot
  73. {
  74. field[bugIndex] = 0;
  75. field[positionAfterMove] = 1;
  76. trigger = true;
  77. }
  78. else if (field[positionAfterMove] == 1) //there is a lady bug
  79. {
  80. field[bugIndex] = 0;
  81. positionAfterMove -= 1;
  82. }
  83. }
  84. }
  85. }
  86. //Right
  87. else if (direction == "right")
  88. {
  89. if (bugIndex + flyLength >= field.Length) //remove lady bug
  90. {
  91. field[bugIndex] = 0;
  92. }
  93. else
  94. {
  95. var positionAfterMove = bugIndex + flyLength;
  96. bool trigger = false;
  97.  
  98. while (trigger == false)
  99. {
  100. if (positionAfterMove >= field.Length)
  101. {
  102. field[bugIndex] = 0;
  103. break;
  104. }
  105. if (field[positionAfterMove] == 0) //empty spot
  106. {
  107. field[bugIndex] = 0;
  108. field[positionAfterMove] = 1;
  109. trigger = true;
  110. }
  111. else if (field[positionAfterMove] == 1) //there is a lady bug
  112. {
  113. field[bugIndex] = 0;
  114. positionAfterMove += 1;
  115. }
  116. }
  117. }
  118. }
  119. return field;
  120. }
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement