Advertisement
Guest User

Untitled

a guest
Apr 30th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  1. using System;
  2.  
  3. namespace PresentDelivery
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. var presentsCount = int.Parse(Console.ReadLine());
  10. var neighbourhoodSize = int.Parse(Console.ReadLine());
  11. var neighbourhood = new string[neighbourhoodSize, neighbourhoodSize];
  12. var niceCidsCount = 0;
  13.  
  14.  
  15. var santa = new Possition("S");
  16.  
  17. var isInside = true;
  18.  
  19.  
  20. FillUpTheNeighbourhood(neighbourhood, santa, ref niceCidsCount);
  21.  
  22. var commands = Console.ReadLine();
  23.  
  24. while (commands != "Christmas morning")
  25. {
  26.  
  27.  
  28. neighbourhood[santa.Row, santa.Column] = "-";
  29.  
  30. switch (commands)
  31. {
  32. case "left":
  33. santa.Column--;
  34. break;
  35. case "right":
  36. santa.Column++;
  37. break;
  38. case "up":
  39. santa.Row--;
  40. break;
  41. case "down":
  42. santa.Row++;
  43. break;
  44. }
  45.  
  46. isInside = IsInside(santa, neighbourhood);
  47.  
  48. if (!isInside)
  49. {
  50. break;
  51. }
  52.  
  53. if (neighbourhood[santa.Row, santa.Column] == "V")
  54. {
  55. presentsCount--;
  56. neighbourhood[santa.Row, santa.Column] = santa.Sign;
  57. }
  58. else if (neighbourhood[santa.Row, santa.Column] == "X")
  59. {
  60. neighbourhood[santa.Row, santa.Column] = santa.Sign;
  61. }
  62. else if (neighbourhood[santa.Row, santa.Column] == "C")
  63. {
  64. if (neighbourhood[santa.Row, santa.Column + 1] != "-" && neighbourhood[santa.Row, santa.Column + 1] != "C")
  65. {
  66. if (presentsCount > 0)
  67. {
  68. neighbourhood[santa.Row, santa.Column + 1] = "-";
  69. presentsCount--;
  70. }
  71. }
  72.  
  73. if (neighbourhood[santa.Row, santa.Column - 1] != "-" && neighbourhood[santa.Row, santa.Column - 1] != "C")
  74. {
  75. if (presentsCount > 0)
  76. {
  77. neighbourhood[santa.Row, santa.Column - 1] = "-";
  78. presentsCount--;
  79. }
  80. }
  81.  
  82. if (neighbourhood[santa.Row + 1, santa.Column] != "-" && neighbourhood[santa.Row + 1, santa.Column] != "C")
  83. {
  84. if (presentsCount > 0)
  85. {
  86. neighbourhood[santa.Row + 1, santa.Column] = "-";
  87. presentsCount--;
  88. }
  89. }
  90.  
  91. if (neighbourhood[santa.Row - 1, santa.Column] != "-" && neighbourhood[santa.Row - 1, santa.Column] != "C")
  92. {
  93. if (presentsCount > 0)
  94. {
  95. neighbourhood[santa.Row - 1, santa.Column] = "-";
  96. presentsCount--;
  97.  
  98. }
  99. }
  100.  
  101. }
  102.  
  103. if(!isInside || presentsCount <= 0)
  104. {
  105. break;
  106. }
  107.  
  108. commands = Console.ReadLine();
  109. }
  110.  
  111. if(presentsCount <= 0)
  112. {
  113. Console.WriteLine("Santa ran out of presents!");
  114. }
  115.  
  116. if (isInside)
  117. {
  118. neighbourhood[santa.Row, santa.Column] = santa.Sign;
  119. }
  120.  
  121. PrintMatrix(neighbourhood);
  122.  
  123.  
  124. var niceCidsWithNoPressents = NiceCidsLeftWihtNoPresents(neighbourhood);
  125.  
  126. if(niceCidsWithNoPressents != 0)
  127. {
  128. Console.WriteLine($"No presents for {niceCidsWithNoPressents} nice kid/s.");
  129. }
  130. else
  131. {
  132. Console.WriteLine($"Good job, Santa! {niceCidsCount} happy nice kid/s.");
  133. }
  134.  
  135.  
  136. }
  137.  
  138. private static int NiceCidsLeftWihtNoPresents(string[,] neighbourhood)
  139. {
  140. var kids = 0;
  141. for (int row = 0; row < neighbourhood.GetLength(0); row++)
  142. {
  143.  
  144. for (int col = 0; col < neighbourhood.GetLength(1); col++)
  145. {
  146. if (neighbourhood[row,col] == "V")
  147. {
  148. kids++;
  149. }
  150. }
  151. }
  152.  
  153. return kids;
  154. }
  155.  
  156. private static bool IsInside(Possition santa, string[,] neighbourhood)
  157. {
  158. return santa.Column >= 0 && santa.Row >= 0 &&
  159. santa.Column < neighbourhood.GetLength(1)
  160. && santa.Row < neighbourhood.GetLength(0);
  161. }
  162.  
  163. private static void PrintMatrix(string[,] neighbourhood)
  164. {
  165. for (int row = 0; row < neighbourhood.GetLength(0); row++)
  166. {
  167. for (int col = 0; col < neighbourhood.GetLength(1); col++)
  168. {
  169. Console.Write(neighbourhood[row, col] + " ");
  170. }
  171. Console.WriteLine();
  172. }
  173. }
  174.  
  175. private static void FillUpTheNeighbourhood(string[,] neighbourhood, Possition santa, ref int happyCidsCount)
  176. {
  177. for (int row = 0; row < neighbourhood.GetLength(0); row++)
  178. {
  179. var neighbourhoodInfo = Console.ReadLine()
  180. .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  181.  
  182. for (int col = 0; col < neighbourhood.GetLength(1); col++)
  183. {
  184. if (neighbourhoodInfo[col] == "S")
  185. {
  186. santa.Row = row;
  187. santa.Column = col;
  188. }
  189. else if (neighbourhoodInfo[col] == "V")
  190. {
  191. happyCidsCount++;
  192. }
  193.  
  194. neighbourhood[row, col] = neighbourhoodInfo[col];
  195. }
  196. }
  197. }
  198.  
  199. }
  200. public class Possition
  201. {
  202. public Possition(string sign)
  203. {
  204. this.Sign = sign;
  205. }
  206.  
  207. public int Row { get; set; }
  208.  
  209. public int Column { get; set; }
  210.  
  211. public string Sign { get; set; }
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement