Created
March 19, 2013 18:32
-
-
Save philihp/5198813 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sos; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Map.Entry; | |
import java.util.Set; | |
import java.util.StringTokenizer; | |
import java.util.TreeMap; | |
import com.sas.commons.util.ArrayUtils; | |
import com.sas.rtolap.util.ShortArray; | |
public class FIRST | |
{ | |
public static void main(String[] args) throws IOException { | |
Main main = new Main(); | |
ArrayList<Match> matches = main.loadMatches("C:\\public\\frc\\results.csv"); | |
// int score = 0; | |
// for (Match m : matches) { | |
// int newScore = getScore(m, (short) 4824); | |
// score += newScore; | |
// } | |
// System.out.println(score); | |
TreeMap<Short, Team> teams = new TreeMap<Short, Team>(); | |
for (Match m : matches) { | |
for (short team : m.blue) { | |
Team t = teams.get(team); | |
if (t == null) { | |
t = new Team(); | |
t.teamNumber = team; | |
teams.put(t.teamNumber, t); | |
} | |
t.record += getScore(m, team); | |
for (short s : m.red) { | |
t.opponents.add(s); | |
} | |
} | |
for (short team : m.red) { | |
Team t = teams.get(team); | |
if (t == null) { | |
t = new Team(); | |
t.teamNumber = team; | |
teams.put(t.teamNumber, t); | |
} | |
t.record += getScore(m, team); | |
for (short s : m.blue) { | |
t.opponents.add(s); | |
} | |
} | |
} | |
Set<Entry<Short, Team>> teamSet = teams.entrySet(); | |
for (Entry<Short, Team> entry : teamSet) { | |
Team team = entry.getValue(); | |
for (int i = 0; i < team.opponents.size; i++) { | |
team.OR += teams.get(team.opponents.shorts[i]).record; | |
} | |
} | |
for (Entry<Short, Team> entry : teamSet) { | |
Team team = entry.getValue(); | |
for (int i = 0; i < team.opponents.size; i++) { | |
team.OOR += teams.get(team.opponents.shorts[i]).OR; | |
} | |
} | |
int teamCnt = teamSet.size(); | |
int[] teamNmb = new int[teamCnt]; | |
int[] SoS = new int[teamCnt]; | |
int i = 0; | |
for (Entry<Short, Team> entry : teamSet) { | |
Team t = entry.getValue(); | |
teamNmb[i] = t.teamNumber; | |
SoS[i] = 54 * t.OR + t.OOR; | |
i++; | |
} | |
ArrayUtils.sort(SoS, teamNmb); | |
for (i = teamCnt - 1; i >= 0; i--) { | |
System.out.println((teamCnt - i) + ": " + teamNmb[i] + "(" + SoS[i] + ")"); | |
} | |
} | |
public static byte getScore(Match m, short team) { | |
for (short s : m.blue) { | |
if (s == team) { | |
return m.blueResult(); | |
} | |
} | |
for (short s : m.red) { | |
if (s == team) { | |
return m.redResult(); | |
} | |
} | |
return 0; | |
} | |
public ArrayList<Match> loadMatches(String filename) throws IOException { | |
ArrayList<Match> matches = new ArrayList<Match>(84); | |
File file = new File(filename); | |
BufferedReader r = new BufferedReader(new FileReader(file)); | |
String line = r.readLine(); | |
while ((line = r.readLine()) != null) { | |
StringTokenizer t = new StringTokenizer(line, ","); | |
assert t.countTokens() == 10 : t.countTokens(); | |
Match match = new Match(); | |
t.nextToken(); // throw away time; | |
match.match = Byte.parseByte(t.nextToken()); | |
match.red[0] = Short.parseShort(t.nextToken()); | |
match.red[1] = Short.parseShort(t.nextToken()); | |
match.red[2] = Short.parseShort(t.nextToken()); | |
match.blue[0] = Short.parseShort(t.nextToken()); | |
match.blue[1] = Short.parseShort(t.nextToken()); | |
match.blue[2] = Short.parseShort(t.nextToken()); | |
match.score[0] = Short.parseShort(t.nextToken()); | |
match.score[1] = Short.parseShort(t.nextToken()); | |
matches.add(match); | |
} | |
return matches; | |
} | |
public static class Team implements Comparable | |
{ | |
short teamNumber; | |
ShortArray opponents = new ShortArray(27); | |
byte record; | |
int OR = 0; | |
int OOR = 0; | |
@Override | |
public int compareTo(Object o) { | |
Team other = (Team) o; | |
return teamNumber - other.teamNumber; | |
} | |
} | |
public static class Match | |
{ | |
byte match; | |
short red[] = new short[3]; | |
short blue[] = new short[3]; | |
short score[] = new short[2]; | |
public byte redResult() { | |
if (score[0] > score[1]) { | |
return 2; | |
} | |
if (score[0] < score[1]) { | |
return 0; | |
} | |
return 1; | |
} | |
public byte blueResult() { | |
if (score[0] > score[1]) { | |
return 0; | |
} | |
if (score[0] < score[1]) { | |
return 2; | |
} | |
return 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment