Advertisement
Guest User

Untitled

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