Advertisement
Guest User

Untitled

a guest
Jun 21st, 2019
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _01._The_Garden
  5. {
  6. class Program
  7. {
  8.  
  9. static void Main(string[] args)
  10. {
  11. int rows = int.Parse(Console.ReadLine());
  12.  
  13. char[][] matrix = new char[rows][];
  14.  
  15. for (int row = 0; row < rows; row++)
  16. {
  17. char[] vegetablas = Console.ReadLine()
  18. .Split(" ")
  19. .Select(char.Parse)
  20. .ToArray();
  21.  
  22. matrix[row] = new char[vegetablas.Length];
  23.  
  24. for (int col = 0; col < vegetablas.Length; col++)
  25. {
  26. matrix[row][col] = vegetablas[col];
  27. }
  28. }
  29.  
  30.  
  31. int lettuceCounter = 0;
  32. int potatoesCounter = 0;
  33. int carrotsCounter = 0;
  34. int harmerVegetableCount = 0;
  35.  
  36. string input = Console.ReadLine();
  37.  
  38. while (input != "End of Harvest")
  39. {
  40. string[] splitInput = input.Split();
  41.  
  42. string command = splitInput[0];
  43. int rowCommand = int.Parse(splitInput[1]);
  44. int colCommand = int.Parse(splitInput[2]);
  45.  
  46. if (command == "Harvest")
  47. {
  48. if (IsInside(matrix, rowCommand, colCommand))
  49. {
  50. if (matrix[rowCommand][colCommand] == 'L')
  51. {
  52. lettuceCounter++;
  53. matrix[rowCommand][colCommand] = ' ';
  54. }
  55. else if (matrix[rowCommand][colCommand] == 'P')
  56. {
  57. potatoesCounter++;
  58. matrix[rowCommand][colCommand] = ' ';
  59. }
  60. else if (matrix[rowCommand][colCommand] == 'C')
  61. {
  62. carrotsCounter++;
  63. matrix[rowCommand][colCommand] = ' ';
  64. }
  65. }
  66. }
  67. else if (command == "Mole")
  68. {
  69. string directions = splitInput[3];
  70. if (IsInside(matrix, rowCommand, colCommand))
  71. {
  72.  
  73. if (directions == "up")
  74. {
  75. for (int i = rowCommand; i >= 0; i -= 2)
  76. {
  77. if (IsInside(matrix, rowCommand, colCommand)
  78. && matrix[i][colCommand] != ' ')
  79. {
  80. matrix[i][colCommand] = ' ';
  81. harmerVegetableCount++;
  82. }
  83. }
  84. }
  85. else if (directions == "down")
  86. {
  87. for (int i = rowCommand; i < matrix.Length; i += 2)
  88. {
  89. if (IsInside(matrix, rowCommand, colCommand)
  90. && matrix[i][colCommand] != ' ')
  91. {
  92. matrix[i][colCommand] = ' ';
  93. harmerVegetableCount++;
  94. }
  95. }
  96. }
  97. else if (directions == "left")
  98. {
  99. for (int currentCol = colCommand; currentCol >= 0; currentCol -= 2)
  100. {
  101. if (IsInside(matrix, rowCommand, colCommand)
  102. && matrix[rowCommand][currentCol] != ' ')
  103. {
  104. matrix[rowCommand][currentCol] = ' ';
  105. harmerVegetableCount++;
  106. }
  107. }
  108. }
  109. else if (directions == "right")
  110. {
  111. for (int currenCol = colCommand; currenCol < matrix[rowCommand].Length; currenCol += 2)
  112. {
  113. if (IsInside(matrix, rowCommand, colCommand)
  114. && matrix[rowCommand][currenCol] != ' ')
  115. {
  116. matrix[rowCommand][currenCol] = ' ';
  117. harmerVegetableCount++;
  118. }
  119. }
  120. }
  121.  
  122.  
  123. }
  124. }
  125. input = Console.ReadLine();
  126. }
  127. foreach (var garden in matrix)
  128. {
  129. Console.WriteLine(String.Join(" ",garden).TrimEnd());
  130. }
  131.  
  132. Console.WriteLine($"Carrots: {carrotsCounter}");
  133. Console.WriteLine($"Potatoes: {potatoesCounter}");
  134. Console.WriteLine($"Lettuce: {lettuceCounter}");
  135. Console.WriteLine($"Harmed vegetables: {harmerVegetableCount}");
  136. }
  137.  
  138. static bool IsInside(char[][] matrix, int rowCommand, int colCommand)
  139. {
  140. return rowCommand >= 0 && rowCommand < matrix.Length &&
  141. colCommand >= 0 && colCommand < matrix[rowCommand].Length;
  142. }
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement