Guest User

Codeforces rating Estimater

a guest
Jan 19th, 2021
272
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.math.BigInteger;
  5. import java.net.URL;
  6. import java.security.MessageDigest;
  7. import java.security.NoSuchAlgorithmException;
  8. import java.util.Arrays;
  9. import java.util.Scanner;
  10.  
  11. import com.google.gson.Gson;
  12. import com.google.gson.GsonBuilder;
  13. import com.google.gson.JsonObject;
  14.  
  15. public class codeforcesAPI {
  16.     public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
  17.         Scanner scan = new Scanner(System.in);
  18.         String handle = scan.nextLine();
  19.  
  20.         listContest(handle);
  21.  
  22.     }
  23.  
  24.     public static void listContest(String user) throws IOException {
  25.         UserContestRatingHistory hist = getUserContestRatingHistory(user);
  26.         for(contest g : hist.result){
  27.             System.out.print("Finding Personal Seed: ");
  28.             int[] f = seed(user, g.contestId);
  29.             int mean = (int)Math.sqrt(f[0] * f[2]);
  30.             System.out.println("[Done]");
  31.  
  32.             System.out.print("Finding All Seeds: ");
  33.             UserContestRatingChange a = getUserContestRatingChange(g.contestId);
  34.             seedRankRating[] seeds = new seedRankRating[a.result.length];
  35.             int myRate = 0;
  36.             int myNew = 0;
  37.             for(int i = 0;i<a.result.length;i++){
  38.                 String handle = a.result[i].handle;
  39.                 if(handle.equalsIgnoreCase(user)) {
  40.                     myRate = a.result[i].oldRating;
  41.                     myNew = a.result[i].newRating;
  42.                 }
  43.                 int[] q = seed2(handle, g.contestId, a);
  44.                 int curSeed = q[0];
  45.                 seeds[i] = new seedRankRating(curSeed, a.result[i].rank, a.result[i].oldRating);
  46.                 if(i != 0) System.out.print("\b".repeat(((i) +"/"+a.result.length).length() + 2));
  47.                 System.out.print("[" + (i+1) +"/"+a.result.length + "]");
  48.             }
  49.             System.out.print("\b".repeat((a.result.length + "").length()*2 + 3));
  50.             Arrays.sort(seeds);
  51.             int index = Arrays.binarySearch(seeds, new seedRankRating(mean, 1, 1));
  52.             if(index< 0) index = -(index+1);
  53.             int rating = seeds[index].rating;
  54.             int average = (rating + myRate)/2;
  55.             System.out.println("[Done]");
  56.  
  57.  
  58.             System.out.print("[" + g.contestName + "    " + g.contestId + "]:" + " ".repeat(Math.max(65 - g.contestName.length(), 10)));
  59.             System.out.println("Total: " + f[1] + "      Expected: " + f[0] + "     Got: " + f[2] + "     RateGuess: " + average + "   Actual: " + myNew);
  60.         }
  61.     }
  62.  
  63.     public static int[] seed(String name, int id) throws IOException {
  64.         UserContestRatingChange a = getUserContestRatingChange(id);
  65.         int myRate = 0;
  66.         int myRank = 0;
  67.         for(contest g : a.result){
  68.             if(g.handle.equalsIgnoreCase(name)){
  69.                 myRate = g.oldRating;
  70.                 myRank = g.rank;
  71.                 break;
  72.             }
  73.         }
  74.         double curSeed = 0;
  75.         for(contest g : a.result){
  76.             if(g.oldRating > myRate){
  77.                 curSeed += elo(g.oldRating, myRate);
  78.             }
  79.         }
  80.         return new int[] {(int)curSeed, a.result.length, myRank};
  81.     }
  82.  
  83.     public static int[] seed2(String name, int id, UserContestRatingChange a) throws IOException {
  84.         int myRate = 0;
  85.         int myRank = 0;
  86.         for(contest g : a.result){
  87.             if(g.handle.equalsIgnoreCase(name)){
  88.                 myRate = g.oldRating;
  89.                 myRank = g.rank;
  90.                 break;
  91.             }
  92.         }
  93.         double curSeed = 0;
  94.         for(contest g : a.result){
  95.             if(g.oldRating > myRate){
  96.                 curSeed += elo(g.oldRating, myRate);
  97.             }
  98.         }
  99.         return new int[] {(int)curSeed, a.result.length, myRank};
  100.     }
  101.  
  102.     public static UserContestRatingChange getUserContestRatingChange(int id) throws IOException {
  103.         URL oracle = new URL("https://codeforces.com/api/contest.ratingChanges?contestId=" + id);
  104.         BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
  105.         String inputLine;
  106.         String tot = "";
  107.         while ((inputLine = in.readLine()) != null) tot += inputLine;
  108.  
  109.         JsonObject s = new Gson().fromJson(tot, JsonObject.class);
  110.         Gson gson = new GsonBuilder().setPrettyPrinting().create();
  111.         String prettyJson = gson.toJson(s);
  112.         UserContestRatingChange o = gson.fromJson(prettyJson, UserContestRatingChange.class);
  113.         return o;
  114.     }
  115.  
  116.     public static UserContestRatingHistory getUserContestRatingHistory(String user) throws IOException {
  117.         URL oracle = new URL("https://codeforces.com/api/user.rating?handle=" + user);
  118.         BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
  119.         String inputLine;
  120.         String tot = "";
  121.         while ((inputLine = in.readLine()) != null) tot += inputLine;
  122.  
  123.         JsonObject s = new Gson().fromJson(tot, JsonObject.class);
  124.         Gson gson = new GsonBuilder().setPrettyPrinting().create();
  125.         String prettyJson = gson.toJson(s);
  126.         UserContestRatingHistory o = gson.fromJson(prettyJson, UserContestRatingHistory.class);
  127.         return o;
  128.     }
  129.  
  130.     public static int getUserRating(String user) throws IOException {
  131.         UserContestRatingHistory f = getUserContestRatingHistory(user);
  132.         return f.result[f.result.length-1].newRating;
  133.     }
  134.  
  135.     public static double elo(int a, int b) {
  136.  
  137.         double elo = 1/(1 + 10*(((double)a - (double)b)/400));
  138.         if(elo < 0) return -1;
  139.         return (1 - elo);
  140.     }
  141.  
  142.  
  143.     public class UserContestRatingHistory {
  144.         String status;
  145.         contest[] result;
  146.  
  147.         public UserContestRatingHistory(String status, contest[] result) {
  148.             this.status = status;
  149.             this.result = result;
  150.         }
  151.  
  152.         @Override
  153.         public String toString() {
  154.             return "out{" +
  155.                     "status='" + status + '\'' +
  156.                     ", result=" + Arrays.toString(result) +
  157.                     '}';
  158.         }
  159.     }
  160.  
  161.     public class UserContestRatingChange {
  162.         String status;
  163.         contest[] result;
  164.  
  165.         public UserContestRatingChange(String status, contest[] result) {
  166.             this.status = status;
  167.             this.result = result;
  168.         }
  169.  
  170.         @Override
  171.         public String toString() {
  172.             return "out{" +
  173.                     "status='" + status + '\'' +
  174.                     ", result=" + Arrays.toString(result) +
  175.                     '}';
  176.         }
  177.     }
  178.  
  179.     public class contest{
  180.         int contestId;
  181.         String contestName;
  182.         String handle;
  183.         int rank;
  184.         long ratingUpdateTimeSeconds;
  185.         int oldRating;
  186.         int newRating;
  187.  
  188.         public contest(int contestId, String contestName, String handle, int rank, long ratingUpdateTimeSeconds, int oldRating, int newRating) {
  189.             this.contestId = contestId;
  190.             this.contestName = contestName;
  191.             this.handle = handle;
  192.             this.rank = rank;
  193.             this.ratingUpdateTimeSeconds = ratingUpdateTimeSeconds;
  194.             this.oldRating = oldRating;
  195.             this.newRating = newRating;
  196.         }
  197.  
  198.         @Override
  199.         public String toString() {
  200.             return "obj{" +
  201.                     "contestId=" + contestId +
  202.                     ", contestName='" + contestName + '\'' +
  203.                     ", handle='" + handle + '\'' +
  204.                     ", rank=" + rank +
  205.                     ", ratingUpdateTimeSeconds=" + ratingUpdateTimeSeconds +
  206.                     ", oldRating=" + oldRating +
  207.                     ", newRating=" + newRating +
  208.                     '}';
  209.         }
  210.     }
  211.  
  212.     public static class seedRankRating implements Comparable<seedRankRating>{
  213.         int seed;
  214.         int rank;
  215.         int rating;
  216.  
  217.         public seedRankRating(int seed, int rank, int rating) {
  218.             this.seed = seed;
  219.             this.rank = rank;
  220.             this.rating = rating;
  221.         }
  222.  
  223.  
  224.         @Override
  225.         public int compareTo(seedRankRating o) {
  226.             return this.seed - o.seed;
  227.         }
  228.     }
  229.  
  230. }
  231.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×