Advertisement
DaPorkchop

Untitled

Nov 24th, 2020
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. package net.daporkchop;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Random;
  5. import java.util.concurrent.ThreadLocalRandom;
  6. import java.util.stream.IntStream;
  7.  
  8. public class Aufgabe7f {
  9.     public static final int SAMPLES = 10_000;
  10.  
  11.     public static final int CARDS = 597;
  12.     public static final int GROUP_SIZE = 3;
  13.  
  14.     public static final ThreadLocal<int[][]> TL_DECKS = ThreadLocal.withInitial(() -> new int[GROUP_SIZE][CARDS]);
  15.     public static final ThreadLocal<int[]> TL_COUNTS = ThreadLocal.withInitial(() -> new int[GROUP_SIZE]);
  16.  
  17.     public static void main(String[] args) {
  18.         double avgCount = IntStream.range(0, SAMPLES).parallel()
  19.                 .map(_i -> {
  20.                     Random r = ThreadLocalRandom.current();
  21.                     int[][] decks = TL_DECKS.get();
  22.                     for (int[] deck : decks) {
  23.                         Arrays.fill(deck, 0);
  24.                     }
  25.                     int[] counts = TL_COUNTS.get();
  26.                     Arrays.fill(counts, 0);
  27.  
  28.                     int finishedDecks = 0;
  29.                     int purchasedCount = 0;
  30.  
  31.                     while (finishedDecks < GROUP_SIZE) {
  32.                         for (int player = 0; finishedDecks < GROUP_SIZE && player < GROUP_SIZE; player++) {
  33.                             if (counts[player] == CARDS) {
  34.                                 continue;
  35.                             }
  36.  
  37.                             int card = r.nextInt(CARDS);
  38.                             if (decks[player][card]++ == 0) { //we've drawn the card for the first time
  39.                                 if (++counts[player] == CARDS) {
  40.                                     finishedDecks++;
  41.                                 }
  42.                             } else { //we already have the card, attempt to trade
  43.                                 TRADE:
  44.                                 for (int p = 0; p < GROUP_SIZE; p++) { //search for a player we can trade with
  45.                                     if (p == player || counts[p] == CARDS || decks[p][card] != 0) {
  46.                                         continue;
  47.                                     }
  48.                                     for (int c = 0; c < CARDS; c++) { //search for a card they can give us
  49.                                         if (decks[player][c] == 0 //we don't have the card
  50.                                                 && decks[p][c] > 1) { //they have multiple instances of the card
  51.                                             decks[player][c] = 1;
  52.                                             decks[p][c]--;
  53.                                             decks[p][card] = 1;
  54.                                             if (++counts[player] == CARDS) {
  55.                                                 finishedDecks++;
  56.                                             }
  57.                                             if (++counts[p] == CARDS) {
  58.                                                 finishedDecks++;
  59.                                             }
  60.                                             break TRADE;
  61.                                         }
  62.                                     }
  63.                                 }
  64.                             }
  65.                             purchasedCount++;
  66.                         }
  67.                     }
  68.  
  69.                     return purchasedCount;
  70.                 })
  71.                 .average().orElseThrow();
  72.  
  73.         System.out.printf("purchased an average of %.2f cards (%.2f cards per person)\n", avgCount, avgCount / GROUP_SIZE);
  74.     }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement