Skip to content

Instantly share code, notes, and snippets.

@dmadisetti
Last active August 29, 2015 14:26
Show Gist options
  • Save dmadisetti/7fee44656c96291a61e5 to your computer and use it in GitHub Desktop.
Save dmadisetti/7fee44656c96291a61e5 to your computer and use it in GitHub Desktop.
import java.util.Map;
import java.util.Arrays;
// Our constraints
int MAX = 100;
int MIN = -10;
int SUM = 60;
// IO
PrintWriter output;
String fileLoad[];
int submissions;
// Hold together everything
HashMap<String,Player> duped = new HashMap<String,Player>();
Player contenders[];
class Player {
int wins = 0;
int faces[];
String row = "";
// Means we couldn't run this conncurrently for race condition worries
// Will be fine for this
int score = 0;
Player(int mFaces[]) {
faces = mFaces;
}
int rand(){
return this.faces[int(random(this.faces.length))];
}
boolean check(Player Z ,int x, int y){
score += x > y ? x - y : 0;
if (score >= 100) {
wins += 1;
Z.row += "| L" + TAB;
row += "| W" + TAB;
return true;
}
return false;
}
String toString(int i){
return "Die " + i + TAB + "| " + wins + TAB + row + ENTER;
}
}
class Game{
boolean over = false;
Player A,B;
Game(Player a, Player b){
A = a;
B = b;
A.score = 0;
B.score = 0;
}
void fight(){
int a = A.rand();
int b = B.rand();
over = A.check(B, a, b) || B.check(A, b, a);
}
}
// Probably some smarter way to do this
// Almost certain they're all unique
String hash(int nums[]){
Arrays.sort(nums);
String val = "";
for (int j=0; j < nums.length; j++) {
val += nums[j];
}
return val;
}
void setup() {
output = createWriter("results.txt");
fileLoad = loadStrings("diceEntrants.txt");
submissions = fileLoad.length;
// Parse and Eliminate
for (int i=0; i < submissions; i++) {
String row[][];
int die[] = new int[6];
int sum = 0;
int min = MAX;
int max = MIN;
row = matchAll(fileLoad[i], "\\[([-0-9]+)\\]");
for (int j=0; j < 6; j++) {
die[j] = int(row[j][1]);
sum += die[j];
if(die[j] < min) min = die[j];
if(die[j] > max) max = die[j];
}
// Verify + Cheap hack to remove dupes
if(sum == SUM && min >= MIN && max <= MAX)
duped.put(hash(die), new Player(die));
}
// Build table and run
String header = "RESULTS"+TAB+"| WINS"+TAB;
String seperator = ENTER + "--------|";
String response = "";
contenders = duped.values().toArray(new Player[duped.size()]);
for (int i = 0; i < contenders.length; i++) {
seperator += "--------|";
header += "| " + (i+1) + TAB;
for (int j = i; j < contenders.length; j++) {
if (i != j){
Game game = new Game(contenders[i],contenders[j]);
while (!game.over) {
game.fight();
}
} else {
contenders[i].row += "| X" + TAB;
}
}
response += contenders[i].toString(i+1);
}
// Spit it all out
response = header + seperator + ENTER + response;
println(response);
output.println(response);
output.flush(); // Write the remaining data
output.close(); // Finish the file
exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment