Skip to content

Instantly share code, notes, and snippets.

@tomerun
Created November 4, 2014 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomerun/aedbe6a780fe0a762c9b to your computer and use it in GitHub Desktop.
Save tomerun/aedbe6a780fe0a762c9b to your computer and use it in GitHub Desktop.
CODE RUNNER予選B
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static final String TOKEN = "CDUWVA9HWVM99WL3JOX5TH501IZZH4S9";
static final String BASE = "https://game.coderunner.jp/";
static final int UPPER = 3000;
static double[][] happy = new double[100][100];
static double[][] dust = new double[100][100];
public static void main(String[] args) throws Exception {
initDist();
int prevRoom = -1;
int[] damages = new int[100];
int maxI = 0;
OUT: while (true) {
int room = getRoom();
if (room != prevRoom) {
Arrays.fill(damages, 0);
prevRoom = room;
for (int i = 0; i < 100; ++i) {
wait1s();
Info info = new Info();
if (info.room != prevRoom) {
break;
}
for (int j = 0; j < 100; ++j) {
if (damages[j] == 0 && info.history[j] > 0) {
damages[j] = info.history[j];
System.out.println("room:" + room);
System.out.println("skill:" + j + " damage:" + damages[j] + " (others)");
System.out.println();
if (damages[j] >= UPPER) {
maxI = j;
continue OUT;
}
}
}
int skill = findNextSkill(damages);
if (skill == -1) break;
damages[skill] = Integer.parseInt(attack(skill).trim());
System.out.println("room:" + info.room);
System.out.println("skill:" + skill + " damage:" + damages[skill]);
System.out.println();
wait1s();
if (damages[skill] >= UPPER) break;
}
maxI = 0;
for (int i = 0; i < 100; ++i) {
if (damages[i] > damages[maxI]) {
maxI = i;
}
}
} else {
int damage = Integer.parseInt(attack(maxI).trim());
System.out.println("room:" + room);
System.out.println("skill:" + maxI + " damage:" + damage);
System.out.println();
wait1s();
}
}
}
static int findNextSkill(int[] damage) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < 100; ++i) {
if (damage[i] != 0) min = Math.min(min, damage[i]);
}
double[] val = new double[100];
for (int i = 0; i < 100; ++i) {
if (damage[i] == 0) continue;
if (damage[i] < min * 1.1) {
for (int j = 0; j < 100; ++j) {
val[j] += dust[i][j];
}
} else if (damage[i] < UPPER) {
for (int j = 0; j < 100; ++j) {
val[j] += happy[i][j];
}
}
}
int ret = -1;
double maxV = -999999;
for (int i = 0; i < 100; ++i) {
if (damage[i] != 0) continue;
if (val[i] > maxV) {
maxV = val[i];
ret = i;
}
}
return ret;
}
static class Info {
int room;
int[] history = new int[100];
Info() throws Exception {
String info = query("https://game.coderunner.jp/info?token=CDUWVA9HWVM99WL3JOX5TH501IZZH4S9&filter=all&style=text");
String[] elem = info.split("\\n");
room = Integer.parseInt(elem[5]);
for (int i = 0; i < elem.length; ++i) {
if (elem[i].equals("history")) {
for (int j = i + 1; j < elem.length; ++j) {
String[] vals = elem[j].trim().split(" ");
int skill = Integer.parseInt(vals[1]);
int damage = Integer.parseInt(vals[2]);
history[skill] = damage;
}
break;
}
}
}
}
static int getRoom() throws Exception {
String info = query("https://game.coderunner.jp/info?token=CDUWVA9HWVM99WL3JOX5TH501IZZH4S9&filter=you&style=text");
String[] elem = info.split("\\n");
return Integer.parseInt(elem[4]);
}
static String attack(int skill) throws Exception {
String url = BASE + "attack?token=" + TOKEN + "&skill=" + skill;
return query(url);
}
public static String query(String _url) throws IOException {
URL url = new URL(_url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStreamReader isr = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(isr);
String res = "";
String line = null;
while ((line = reader.readLine()) != null) {
res += line + "\n";
}
return res;
}
return "";
}
static void wait1s() {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {}
}
static void initDist() throws Exception {
try (Scanner sc = new Scanner(new File("dist.txt"))) {
for (int i = 0; i < 100; ++i) {
for (int j = 0; j < 100; ++j) {
happy[i][j] = sc.nextDouble();
}
}
for (int i = 0; i < 100; ++i) {
for (int j = 0; j < 100; ++j) {
dust[i][j] = sc.nextDouble();
}
}
}
}
}
import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Analyzer {
static Scanner sc = new Scanner(System.in);
static Pattern patRoom = Pattern.compile("room:(\\d+)");
static Pattern patAttack = Pattern.compile("skill:(\\d+) damage:(\\d+).*");
static HashMap<Integer, Room> map = new HashMap<>();
public static void main(String[] args) {
int room = 0;
while (sc.hasNextLine()) {
String line = sc.nextLine();
Matcher matcher = patRoom.matcher(line);
if (matcher.matches()) {
room = Integer.parseInt(matcher.group(1));
} else {
matcher = patAttack.matcher(line);
if (matcher.matches()) {
int skill = Integer.parseInt(matcher.group(1));
int damage = Integer.parseInt(matcher.group(2));
add(room, skill, damage);
}
}
}
analyze();
}
static void analyze() {
int[][] cand = new int[100][100];
int[][] dust = new int[100][100];
int[][] count = new int[100][100];
for (Room r : map.values()) {
int[] dam = r.damage;
int min = Integer.MAX_VALUE;
for (int i = 0; i < 100; ++i) {
if (dam[i] != 0) min = Math.min(min, dam[i]);
}
if (min > 1000) continue;
int[] norm = new int[100];
for (int i = 0; i < 100; ++i) {
if (dam[i] == 0) continue;
if (dam[i] == min) {
norm[i] = 1;
} else if (dam[i] <= min * 2) {
norm[i] = 2;
} else if (dam[i] <= min * 4) {
norm[i] = 2;
} else if (dam[i] <= min * 10) {
norm[i] = 2;
} else if (dam[i] <= min * 40) {
norm[i] = 3;
} else if (dam[i] <= min * 100) {
norm[i] = 4;
} else if (dam[i] <= min * 400) {
norm[i] = 5;
} else {
norm[i] = 8;
}
}
for (int i = 0; i < 100; ++i) {
if (norm[i] == 0) continue;
if (norm[i] == 2) {
for (int j = 0; j < 100; ++j) {
if (norm[j] == 0) continue;
if (norm[j] > 2) {
cand[i][j] += (norm[j] - 2) * 20;
} else {
cand[i][j] += (norm[j] - 2) * 1;
}
count[i][j]++;
}
} else if (norm[i] == 1) {
for (int j = 0; j < 100; ++j) {
if (norm[j] == 0) continue;
if (norm[j] == 1) dust[i][j] -= 1;
count[i][j]++;
}
}
}
}
for (int i = 0; i < 100; ++i) {
for (int j = 0; j < 100; ++j) {
System.out.printf("%.3f ", count[i][j] == 0 ? 0 : 1.0 * cand[i][j] / count[i][j]);
}
System.out.println();
}
System.out.println();
for (int i = 0; i < 100; ++i) {
for (int j = 0; j < 100; ++j) {
System.out.printf("%.3f ", count[i][j] == 0 ? 0 : 1.0 * dust[i][j] / count[i][j]);
}
System.out.println();
}
}
static void add(int room, int skill, int damage) {
if (!map.containsKey(room)) {
map.put(room, new Room(room));
}
Room r = map.get(room);
r.damage[skill] = damage;
}
static class Room {
int id;
int[] damage = new int[100];
Room(int id) {
this.id = id;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment