Advertisement
simonradev

Untitled

Jun 24th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | None | 0 0
  1. namespace SecondNature
  2. 2
  3. {
  4. 3
  5.     using System;
  6. 4
  7.     using System.Collections.Generic;
  8. 5
  9.     using System.Linq;
  10. 6
  11.     using System.Text;
  12. 7
  13.     using System.Threading.Tasks;
  14. 8
  15.    
  16. 9
  17.     public class StartUp
  18. 10
  19.     {
  20. 11
  21.         public static void Main()
  22. 12
  23.         {
  24. 13
  25.             Stack<int> edelFlowers = new Stack<int>(ReadSequenceFromTheConsole().Reverse());
  26. 14
  27.             Stack<int> buckets = new Stack<int>(ReadSequenceFromTheConsole());
  28. 15
  29.  
  30. 16
  31.             Queue<int> secondNatureFlowers = new Queue<int>();
  32. 17
  33.  
  34. 18
  35.             while (CollectionIsNotEmpty(edelFlowers) && CollectionIsNotEmpty(buckets))
  36. 19
  37.             {
  38. 20
  39.                 int currentFlower = edelFlowers.Peek();
  40. 21
  41.                 int currentBucket = buckets.Peek();
  42. 22
  43.  
  44. 23
  45.                 int weissDust = currentFlower - currentBucket;
  46. 24
  47.                
  48. 25
  49.                 if (weissDust == 0)
  50. 26
  51.                 {
  52. 27
  53.                     //Add the second nature flower by removing it from the others.
  54. 28
  55.                     secondNatureFlowers.Enqueue(edelFlowers.Pop());
  56. 29
  57.  
  58. 30
  59.                     //Remove the bucket of water
  60. 31
  61.                     buckets.Pop();
  62. 32
  63.                 }
  64. 33
  65.                 else if (weissDust > 0)
  66. 34
  67.                 {
  68. 35
  69.                     //not enough water
  70. 36
  71.                     //replace the current flower with its decreased value
  72. 37
  73.                     edelFlowers.Pop();
  74. 38
  75.                     edelFlowers.Push(weissDust);
  76. 39
  77.  
  78. 40
  79.                     //get the new bucket
  80. 41
  81.                     buckets.Pop();
  82. 42
  83.                 }
  84. 43
  85.                 else if (weissDust < 0)
  86. 44
  87.                 {
  88. 45
  89.                     //too much water
  90. 46
  91.                     //remove the current flower because it bloomed
  92. 47
  93.                     edelFlowers.Pop();
  94. 48
  95.  
  96. 49
  97.                     //remove the current bucket
  98. 50
  99.                     buckets.Pop();
  100. 51
  101.  
  102. 52
  103.                     int leftOverWeissDust = Math.Abs(weissDust);
  104. 53
  105.                     //add the remaining water to the next flower
  106. 54
  107.                     if (buckets.Count == 0)
  108. 55
  109.                     {
  110. 56
  111.                         buckets.Push(leftOverWeissDust);
  112. 57
  113.                     }
  114. 58
  115.                     else
  116. 59
  117.                     {
  118. 60
  119.                         buckets.Push(buckets.Pop() + leftOverWeissDust);
  120. 61
  121.                     }
  122. 62
  123.                 }
  124. 63
  125.             }
  126. 64
  127.  
  128. 65
  129.             StringBuilder result = new StringBuilder();
  130. 66
  131.             if (buckets.Count > 0)
  132. 67
  133.             {
  134. 68
  135.                 result.AppendLine(JoinCollectionWithSpace(buckets));
  136. 69
  137.             }
  138. 70
  139.             else
  140. 71
  141.             {
  142. 72
  143.                 result.AppendLine(JoinCollectionWithSpace(edelFlowers));
  144. 73
  145.             }
  146. 74
  147.  
  148. 75
  149.             if (secondNatureFlowers.Count > 0)
  150. 76
  151.             {
  152. 77
  153.                 result.AppendLine(JoinCollectionWithSpace(secondNatureFlowers));
  154. 78
  155.             }
  156. 79
  157.             else
  158. 80
  159.             {
  160. 81
  161.                 result.AppendLine("None");
  162. 82
  163.             }
  164. 83
  165.  
  166. 84
  167.             Console.Write(result);
  168. 85
  169.         }
  170. 86
  171.  
  172. 87
  173.         private static bool CollectionIsNotEmpty(Stack<int> collection)
  174. 88
  175.         {
  176. 89
  177.             return collection.Count > 0;
  178. 90
  179.         }
  180. 91
  181.  
  182. 92
  183.         private static string JoinCollectionWithSpace(IEnumerable<int> collection)
  184. 93
  185.         {
  186. 94
  187.             return string.Join(" ", collection);
  188. 95
  189.         }
  190. 96
  191.  
  192. 97
  193.         private static IEnumerable<int> ReadSequenceFromTheConsole()
  194. 98
  195.         {
  196. 99
  197.             return Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
  198. 100
  199.         }
  200. 101
  201.     }
  202. 102
  203. }
  204. 103
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement