Advertisement
Guest User

C# LadyBugs

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