Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. /*
  2. Three people are playing the following betting game.
  3. Every five minutes, a turn takes place in which a random player rests and the other two bet
  4. against one another with all of their money.
  5. The player with the smaller amount of money always wins,
  6. doubling his money by taking it from the loser.
  7. For example, if the initial amounts of money are 1, 4, and 6,
  8. then the result of the first turn can be either
  9. 2,3,6 (1 wins against 4);
  10. 1,8,2 (4 wins against 6); or
  11. 2,4,5 (1 wins against 6).
  12. If two players with the same amount of money play against one another,
  13. the game immediately ends for all three players.
  14. Find initial amounts of money for the three players, where none of the three has more than 255,
  15. and in such a way that the game cannot end in less than one hour. (So at least 12 turns)
  16. In the example above (1,4,6), there is no way to end the game in less than 15 minutes.
  17. All numbers must be positive integers.
  18. */
  19.  
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.List;
  23.  
  24. public class Puzzle {
  25.  
  26. public static void main(String[] argv) {
  27. Puzzle puzzle = new Puzzle();
  28. puzzle.run();
  29. }
  30.  
  31. private int DESIRED_TURNS = 11;
  32. private int MAX_MONEY = 256;
  33. private List<int[]> matches = Collections.synchronizedList(new ArrayList<int[]>());
  34. public enum Wait { PLAYER1, PLAYER2, PLAYER3 }
  35.  
  36. private void run() {
  37. long tic = System.currentTimeMillis();
  38. Wait[] events = this.calcEvents();
  39. System.out.println("Generate Events Time: " + (System.currentTimeMillis() - tic));
  40.  
  41. tic = System.currentTimeMillis();
  42. List<int[]> combinations = new ArrayList<>(2829056);
  43. for (int x = 1; x < MAX_MONEY; x++) {
  44. for (int y = x; y < MAX_MONEY; y++) {
  45. for (int z = y; z < MAX_MONEY; z++) {
  46. int[] intArray = {x, y, z};
  47. combinations.add(intArray);
  48. }
  49. }
  50. }
  51. System.out.println("Generate Combinations Time: " + (System.currentTimeMillis() - tic));
  52.  
  53. tic = System.currentTimeMillis();
  54. combinations.parallelStream().forEach(x -> this.playGames(x[0], x[1], x[2], events));
  55.  
  56. for (int[] matchReturn: matches) {
  57. System.out.println(matchReturn[0] + " " + matchReturn[1] + " " + matchReturn[2]);
  58. }
  59.  
  60. System.out.println("Calculate Matches Time: " + (System.currentTimeMillis() - tic));
  61. }
  62.  
  63. private void playGames(int x, int y, int z, Wait[] events) {
  64.  
  65. int player1 = x;
  66. int player2 = y;
  67. int player3 = z;
  68. int count = 0;
  69.  
  70. for (Wait e: events) {
  71. if (player1 == player2 || player1 == player3 || player2 == player3) return;
  72.  
  73. if (e == Wait.PLAYER1) {
  74. if (player2 > player3) {
  75. player2 = player2 - player3;
  76. player3 = player3 * 2;
  77. }
  78. else {
  79. player3 = player3 - player2;
  80. player2 = player2 * 2;
  81. }
  82. }
  83. else if (e == Wait.PLAYER2) {
  84. if (player1 > player3) {
  85. player1 = player1 - player3;
  86. player3 = player3 * 2;
  87. }
  88. else {
  89. player3 = player3 - player1;
  90. player1 = player1 * 2;
  91. }
  92. }
  93. else {
  94. if (player1 > player2) {
  95. player1 = player1 - player2;
  96. player2 = player2 * 2;
  97. }
  98. else {
  99. player2 = player2 - player1;
  100. player1 = player1 * 2;
  101. }
  102. }
  103.  
  104. if (count == 11) {
  105. player1 = x;
  106. player2 = y;
  107. player3 = z;
  108. count = 0;
  109. }
  110. count++;
  111. }
  112.  
  113. int[] match = {x, y, z};
  114. this.matches.add(match);
  115. }
  116.  
  117. private Wait[] calcEvents() {
  118. Wait[] flatEvents = new Wait[1948617];
  119.  
  120. List<Wait[]> events = this.calcEvents(new Wait[this.DESIRED_TURNS], 0);
  121.  
  122. int count = 0;
  123. for (Wait[] event: events) {
  124. for (Wait e: event) {
  125. flatEvents[count] = e;
  126. count++;
  127. }
  128. }
  129.  
  130. return flatEvents;
  131. }
  132.  
  133. private List<Wait[]> calcEvents(Wait[] current, int turn) {
  134. if (turn == this.DESIRED_TURNS) {
  135. return new ArrayList<Wait[]>() {{
  136. add(current);
  137. }};
  138. }
  139.  
  140. Wait[] one = new Wait[this.DESIRED_TURNS];
  141. System.arraycopy(current, 0, one, 0, current.length);
  142. one[turn] = Wait.PLAYER1;
  143.  
  144. Wait[] two = new Wait[this.DESIRED_TURNS];
  145. System.arraycopy(current, 0, two, 0, current.length);
  146. two[turn] = Wait.PLAYER2;
  147.  
  148. Wait[] three = new Wait[this.DESIRED_TURNS];
  149. System.arraycopy(current, 0, three, 0, current.length);
  150. three[turn] = Wait.PLAYER3;
  151.  
  152. turn++;
  153.  
  154. List<Wait[]> ints1 = calcEvents(one, turn);
  155. List<Wait[]> ints2 = calcEvents(two, turn);
  156. List<Wait[]> ints3 = calcEvents(three, turn);
  157.  
  158. ArrayList<Wait[]> toReturn = new ArrayList<>();
  159. toReturn.addAll(ints1);
  160. toReturn.addAll(ints2);
  161. toReturn.addAll(ints3);
  162.  
  163. return toReturn;
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement