Advertisement
Guest User

Number Wars

a guest
Aug 8th, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _03.NumberWars
  9. {
  10. public class NumberWars
  11. {
  12. public static void Main()
  13. {
  14. var firstQueue = new Queue<string>(Console.ReadLine().Split());
  15. var secondQueue = new Queue<string>(Console.ReadLine().Split());
  16. var turns = 0;
  17.  
  18. var list = new List<string>();
  19.  
  20. while (firstQueue.Count > 0 && secondQueue.Count > 0 && turns < 1000000)
  21. {
  22. turns++;
  23.  
  24. var firstCurrentCard = firstQueue.Dequeue();
  25. var firstPower = int.Parse(firstCurrentCard.Substring(0, firstCurrentCard.Length - 1));
  26.  
  27. var secondCurrentCard = secondQueue.Dequeue();
  28. var secondPower = int.Parse(secondCurrentCard.Substring(0, secondCurrentCard.Length - 1));
  29.  
  30. if (firstPower > secondPower)
  31. {
  32. firstQueue.Enqueue(firstCurrentCard);
  33. firstQueue.Enqueue(secondCurrentCard);
  34. }
  35.  
  36. else if (firstPower < secondPower)
  37. {
  38. secondQueue.Enqueue(secondCurrentCard);
  39. secondQueue.Enqueue(firstCurrentCard);
  40. }
  41.  
  42. else
  43. {
  44. while (firstQueue.Count >= 3 && secondQueue.Count >= 3)
  45. {
  46. var sb1 = new StringBuilder();
  47. var firstCard1 = firstQueue.Dequeue();
  48. var secondCard1 = firstQueue.Dequeue();
  49. var thirdCard1 = firstQueue.Dequeue();
  50.  
  51. list.Add(firstCard1);
  52. list.Add(secondCard1);
  53. list.Add(thirdCard1);
  54.  
  55. sb1.Append(firstCard1.Last());
  56. sb1.Append(secondCard1.Last());
  57. sb1.Append(thirdCard1.Last());
  58.  
  59. var sum1 = 0;
  60.  
  61. for (int i = 0; i < sb1.ToString().Length; i++)
  62. {
  63. sum1 += (int)(sb1.ToString()[i] - 96);
  64. }
  65.  
  66. var sb2 = new StringBuilder();
  67.  
  68. var firstCard2 = secondQueue.Dequeue();
  69. var secondCard2 = secondQueue.Dequeue();
  70. var thirdCard2 = secondQueue.Dequeue();
  71.  
  72. sb2.Append(firstCard2.Last());
  73. sb2.Append(secondCard2.Last());
  74. sb2.Append(thirdCard2.Last());
  75.  
  76. list.Add(firstCard2);
  77. list.Add(secondCard2);
  78. list.Add(thirdCard2);
  79.  
  80. var sum2 = 0;
  81.  
  82. for (int i = 0; i < sb2.ToString().Length; i++)
  83. {
  84. sum2 += (int)(sb2.ToString()[i] - 96);
  85. }
  86.  
  87. list = list.OrderByDescending(x => x).ToList();
  88.  
  89. if (sum1 > sum2)
  90. {
  91. for (int i = 0; i < list.Count; i++)
  92. {
  93. firstQueue.Enqueue(list[i]);
  94. }
  95. }
  96.  
  97. else if (sum1 < sum2)
  98. {
  99. for (int i = 0; i < list.Count; i++)
  100. {
  101. secondQueue.Enqueue(list[i]);
  102. }
  103. }
  104.  
  105. else
  106. {
  107. continue;
  108. }
  109.  
  110. }
  111. }
  112. }
  113.  
  114. if (turns== 1000000)
  115. {
  116. if (firstQueue.Count> secondQueue.Count)
  117. {
  118. Console.WriteLine($"First player wins after {turns} turns");
  119. }
  120.  
  121. else if (firstQueue.Count < secondQueue.Count)
  122. {
  123. Console.WriteLine($"Second player wins after {turns} turns");
  124. }
  125. else
  126. {
  127. Console.WriteLine($"Draw after {turns} turns");
  128. }
  129. }
  130.  
  131. else
  132. {
  133. if (firstQueue.Count > 0)
  134. {
  135. Console.WriteLine($"First player wins after {turns} turns");
  136. }
  137. else if (secondQueue.Count > 0)
  138. {
  139. Console.WriteLine($"Second player wins after {turns} turns");
  140. }
  141. else
  142. {
  143. Console.WriteLine($"Draw after {turns} turns");
  144. }
  145. }
  146.  
  147. }
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement