Skip to content

Instantly share code, notes, and snippets.

@Izaron
Created September 25, 2015 17:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Izaron/45fb719553b1a812a089 to your computer and use it in GitHub Desktop.
Save Izaron/45fb719553b1a812a089 to your computer and use it in GitHub Desktop.
Codeforces Statistics
package com.izaron.codeforces.json;
import java.io.*;
import java.net.*;
import java.util.*;
import org.json.*;
import sun.misc.Sort;
public class Main {
static Map<String, Integer> langMap = new HashMap<>();
static List<Score> stats = new ArrayList<>();
static class Score implements Comparable<Score> {
int score;
String name;
public Score(int score, String name) {
this.score = score;
this.name = name;
}
@Override
public int compareTo(Score o) {
return score < o.score ? -1 : score > o.score ? 1 : 0;
}
}
public static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
try {
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
return buffer.toString();
} catch (Exception e) {
return null;
} finally {
if (reader != null)
reader.close();
}
}
public static void updateStatistics(int contestNumber) throws Exception {
String code = readUrl(" http://codeforces.com/api/contest.status?contestId=" + contestNumber);
if(code == null)
return;
JSONObject obj = new JSONObject(code);
JSONArray result = obj.getJSONArray("result");
for(int i = 0; i < result.length(); i++) {
JSONObject submission = result.getJSONObject(i);
if(!submission.getJSONObject("author").getString("participantType").equals("CONTESTANT") &&
!submission.getJSONObject("author").getString("participantType").equals("OUT_OF_COMPETITION"))
continue;
if(!submission.getString("verdict").equals("OK"))
continue;
String lang = submission.getString("programmingLanguage");
int count = 0;
if(langMap.containsKey(lang)) {
count = langMap.get(lang);
langMap.remove(lang);
}
langMap.put(lang, count + 1);
}
System.out.println(contestNumber + " contest counted...");
}
public static void mergeLangs(String result, List<String> dialectList) {
int count = 0;
for(String dialect : dialectList) {
if (langMap.containsKey(dialect)) {
count += langMap.get(dialect);
langMap.remove(dialect);
}
}
langMap.put(result, count);
}
public static void mergeLangs() {
// C++
List<String> cppList = new ArrayList<>();
cppList.add("GNU C++");
cppList.add("GNU C++11");
cppList.add("GNU C++0x");
cppList.add("MS C++");
mergeLangs("C++", cppList);
// Java
List<String> javaList = new ArrayList<>();
javaList.add("Java 8");
javaList.add("Java 7");
javaList.add("Java 6");
mergeLangs("Java", javaList);
// C
List<String> cList = new ArrayList<>();
cList.add("GNU C");
cList.add("GNU C11");
mergeLangs("C", cList);
// Python
List<String> pythonList = new ArrayList<>();
pythonList.add("Python 2");
pythonList.add("Python 3");
mergeLangs("Python", pythonList);
// C#
List<String> charpList = new ArrayList<>();
charpList.add("MS C#");
charpList.add("Mono C#");
mergeLangs("C#", charpList);
// PyPy
List<String> pypyList = new ArrayList<>();
pypyList.add("PyPy 2");
pypyList.add("PyPy 3");
mergeLangs("PyPy", pypyList);
}
public static void sortStatistics() {
for (Map.Entry<String, Integer> entry : langMap.entrySet())
stats.add(new Score(entry.getValue(), entry.getKey()));
Collections.sort(stats);
Collections.reverse(stats);
}
public static void main(String[] args) throws Exception {
int firstContest = 1, lastContest = 51;
for(int i = firstContest; i <= lastContest; i++)
updateStatistics(i);
mergeLangs();
sortStatistics();
System.out.println();
for (Score score : stats)
System.out.println(score.name + " - " + score.score);
// Print in convenient format for pie chart maker
System.out.println();
for (Score score : stats)
System.out.print(score.name + " ");
System.out.println();
for (Score score : stats)
System.out.print(score.score + " ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment