Skip to content

Instantly share code, notes, and snippets.

@takapt
Created November 2, 2014 14:35
Show Gist options
  • Save takapt/21d41d0767e76e01702b to your computer and use it in GitHub Desktop.
Save takapt/21d41d0767e76e01702b to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <fstream>
#include <bitset>
using namespace std;
#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
template <typename T> void print_container(ostream& os, const T& c) { const char* _s = " "; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); foreach (it, c) { os << *it; if (it != last) os << _s; } } }
template <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; }
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; }
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T> void print(T a, int n, const string& split = " ") { for (int i = 0; i < n; i++) { cout << a[i]; if (i + 1 != n) cout << split; } cout << endl; }
template <typename T> void print2d(T a, int w, int h, int width = -1, int br = 0) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }
template <typename T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
#define dump(v) (cerr << #v << ": " << v << endl)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define clr(a, x) memset(a, x, sizeof(a))
#define sz(a) ((int)(a).size())
#define mp(a, b) make_pair(a, b)
#define ten(n) ((long long)(1e##n))
template <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }
template <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }
template <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }
template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }
template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }
void fast_io() { cin.tie(0); ios::sync_with_stdio(false); }
bool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }
typedef long long ll;
typedef pair<int, int> pint;
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };
#ifdef _MSC_VER
#include <Windows.h>
#else
#include <unistd.h>
#endif
using namespace std;
class Random
{
private:
unsigned int x, y, z, w;
public:
Random(unsigned int x
, unsigned int y
, unsigned int z
, unsigned int w)
: x(x), y(y), z(z), w(w) { }
Random()
: x(123456789), y(362436069), z(521288629), w(88675123) { }
Random(unsigned int seed)
: x(123456789), y(362436069), z(521288629), w(seed) { }
unsigned int next()
{
unsigned int t = x ^ (x << 11);
x = y;
y = z;
z = w;
return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}
int next_int() { return next(); }
// [0, upper)
int next_int(int upper) { return next() % upper; }
// [low, high]
int next_int(int low, int high) { return next_int(high - low + 1) + low; }
double next_double(double upper) { return upper * next() / UINT_MAX; }
double next_double(double low, double high) { return next_double(high - low) + low; }
template <typename T>
int select(const vector<T>& ratio)
{
T sum = accumulate(ratio.begin(), ratio.end(), (T)0);
T v = next_double(sum) + (T)1e-6;
for (int i = 0; i < (int)ratio.size(); ++i)
{
v -= ratio[i];
if (v <= 0)
return i;
}
return 0;
}
};
// Timer (タイマ)
#ifdef _MSC_VER
#include <Windows.h>
#else
#include <sys/time.h>
#endif
class Timer
{
typedef double time_type;
typedef unsigned int skip_type;
private:
time_type start_time;
time_type elapsed;
#ifdef _MSC_VER
time_type get_ms() { return (time_type)GetTickCount64() / 1000; }
#else
time_type get_ms() { struct timeval t; gettimeofday(&t, NULL); return (time_type)t.tv_sec * 1000 + (time_type)t.tv_usec / 1000; }
#endif
public:
Timer() {}
void start() { start_time = get_ms(); }
time_type get_elapsed() { return elapsed = get_ms() - start_time; }
};
string get(string url) {
#ifdef _MSC_VER
// Windowsは同梱のcurl.exeを適当なフォルダに配置してcurlコマンドを使えるようにしておくこと。
FILE *f = _popen(("C:\\bin\\curl.exe -s \"" + url + "\"").c_str(), "r");
#else
// Linux,MacOS,Cygwinはパッケージ管理システムなどでインストールしてcurlコマンドを使えるようにしておくこと。
FILE *f = popen(("curl -s -k \"" + url + "\"").c_str(), "r");
#endif
if (f == NULL) {
perror("error!");
}
char buf[1024];
string res;
while (!feof(f)) {
if (fgets(buf, 1024, f) == NULL) break;
res += (string)(buf);
}
#ifdef _MSC_VER
_pclose(f);
#else
pclose(f);
#endif
return res;
}
Timer timer;
double last_query = 0;
int attack(int skill)
{
double wait = 10 * 1000 - (timer.get_elapsed() - last_query);
if (wait > 0)
usleep(wait * 100);
static const string token = "CDMJMZJI18HCCDVBU3HO05BAGXVXQWPU";
string url = "https://game.coderunner.jp/attack?token=CDMJMZJI18HCCDVBU3HO05BAGXVXQWPU&skill=";
url += to_s(skill);
int damage = -19;
for (bool ok = false; !ok; )
{
string res = get(url);
ok = true;
for (char c : res)
{
if (!isdigit(c))
{
ok = false;
usleep(100);
break;
}
}
if (ok)
{
damage = to_T<int>(res);
break;
}
}
last_query = timer.get_elapsed();
dump(damage);
return damage;
}
struct Info
{
Info(string res)
: raw(res)
{
stringstream ss(res);
{
string lines[7];
rep(i, 7)
getline(ss, lines[i]);
you_user_id = to_T<int>(lines[1]);
you_user_name = lines[2];
token = lines[3];
mes = lines[4];
room_id = to_T<int>(lines[5]);
score = to_T<int>(lines[6]);
}
{
string members;
getline(ss, members);
// assert(members == "members");
string ww;
while (getline(ss, ww), ww.substr(0, 7) != "history")
{
int user_id;
char buf[256];
sscanf(ww.c_str(), "%d %s", &user_id, buf);
mem_user_id.push_back(user_id);
mem_user_name.push_back(buf);
}
}
// assert(history == "history");
for (;;)
{
// string history;
// getline(ss, history);
string ww;
getline(ss, ww);
if (ww.size() <= 3)
break;
int user_id, skill, damage;
sscanf(ww.c_str(), "%d%d%d", &user_id, &skill, &damage);
hist_user_id.push_back(user_id);
hist_skill.push_back(skill);
hist_damage.push_back(damage);
}
damage.resize(100);
rep(i, 100)
damage[i] = -1;
rep(i, hist_size())
{
damage[hist_skill[i]] = hist_damage[i];
}
write();
}
int mem_size() const
{
return mem_user_id.size();
}
int hist_size() const
{
return hist_user_id.size();
}
string raw;
// you
int you_user_id;
string you_user_name;
string token;
string mes;
int room_id;
int score;
// members
vector<int> mem_user_id;
vector<string> mem_user_name;
// history
vector<int> hist_user_id, hist_skill, hist_damage;
// int damage[100];
vector<int> damage;
void write()
{
fstream fs("room/" + to_s(room_id), fstream::out | fstream::trunc);
rep(i, 100)
fs << damage[i] << endl;
fs.close();
}
};
Info get_info()
{
static const string token = "CDMJMZJI18HCCDVBU3HO05BAGXVXQWPU";
static const string url = "https://game.coderunner.jp/info?token=CDMJMZJI18HCCDVBU3HO05BAGXVXQWPU&style=text";
auto res = get(url);
Info info(res);
return info;
}
void tekitoo()
{
auto info = get_info();
pint a(-1, -1);
rep(i, 100)
upmax(a, pint(info.damage[i], i));
if (a.first <= 4000)
{
// rep(i, 100)
for (int i = 99; i >= 0; --i)
{
if (info.damage[i] == -1)
{
attack(i);
break;
}
}
}
else
{
attack(a.second);
}
}
#include <sys/types.h>
#include <dirent.h>
vector<string> files()
{
DIR* dp = opendir("room");
if (dp == NULL)
abort();
vector<string> f;
struct dirent* dent;
for (;;)
{
dent = readdir(dp);
if (dent == NULL)
break;
if (dent->d_name[0] == '.')
continue;
f.push_back(dent->d_name);
}
closedir(dp);
sleep(1);
return f;
}
vector<int> read_room(const string& room)
{
fstream fs("room/" + room);
vector<int> damage(100, -1);
rep(i, 100)
fs >> damage[i];
return damage;
}
ll attr_g[128][128];
void build_graph()
{
clr(attr_g, 0);
auto rooms = files();
for (string room : rooms)
{
const vector<int> damage = read_room(room);
auto d = damage;
uniq(d);
reverse(all(d));
assert(d.back() == -1);
d.pop_back();
d.pop_back();
if (d.size() <= 1)
continue;
rep(di, sz(d) - 1)
{
const int a = d[di], b = d[di + 1];
const int p = sz(d) - di;
const int same_p = p * p * p;
rep(i, 100) rep(j, 100)
{
if (damage[i] == a && damage[j] == a)
attr_g[i][j] += same_p;
}
const int two_p = p * p;
rep(i, 100) rep(j, 100)
{
if (damage[i] == a && damage[j] == b)
{
attr_g[i][j] += two_p;
attr_g[j][i] += two_p;
}
}
}
}
rep(i, 100)
attr_g[i][i] = -ten(5);
// rep(i, 100)
// {
// vector<pint> a;
// rep(j, 100)
// a.push_back(pint(attr_g[i][j], j));
// sort(rall(a));
// a.erase(a.begin() + 10, a.end());
// dump(a);
// }
}
void gaoo()
{
Info info = get_info();
auto damage = info.damage;
auto d = damage;
uniq(d);
reverse(all(d));
d.pop_back();
if (d[0] >= 10000)
{
rep(i, 100)
{
if (damage[i] == d[0])
{
attack(i);
return;
}
}
}
if (d.size() <= 1)
{
rep(i, 100)
{
if (damage[i] == -1)
{
attack(i);
return;
}
}
}
vector<ll> poi(100);
rep(di, sz(d))
{
const int p = sz(d) - di;
int w = p;
if (di >= sz(d) / 2)
w = -di;
rep(i, 100)
{
if (damage[i] == d[di])
{
rep(j, 100)
{
if (i != j)
poi[j] += w * attr_g[i][j];
}
}
}
}
ll best = -ten(18);
int k = -1;
rep(i, 100)
{
if (damage[i] == -1 && poi[i] > best)
{
best = poi[i];
k = i;
}
}
dump(d);
dump(poi);
dump(best);
if (k == -1)
k = rand() % 100;
attack(k);
}
int main()
{
timer.start();
srand(time(NULL));
rep(i, ten(6))
{
if (i % 10 == 0)
build_graph();
gaoo();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment