Created
December 12, 2015 08:58
-
-
Save tanakh/e871b77c032056a0d3f0 to your computer and use it in GitHub Desktop.
code-runner-2015-final-open
This file contains hidden or 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
#include <iostream> | |
#include <string> | |
#include <sstream> | |
#include <iterator> | |
#include <fstream> | |
#include <vector> | |
#include <map> | |
#include <set> | |
#include <algorithm> | |
#include <cstdlib> | |
#include <cassert> | |
#include <unistd.h> | |
using namespace std; | |
// token TRI8BPTlxmiH8HtPkYNLmduRZR9mSCJB | |
const char *token = "TRI8BPTlxmiH8HtPkYNLmduRZR9mSCJB"; | |
/* | |
仕事受注 http://game.coderunner.jp/taketask?token=[your_token] 0.2秒制限 | |
http://game.coderunner.jp/taketaskJson?token=[your_token] | |
仕事割り当て http://game.coderunner.jp/assign?token=[your_token]&task=[task_id]&worker=[worker_id],[worker_id],[worker_id],... なし | |
仕事外注 http://game.coderunner.jp/outsource?token=[your_token]&task=[task_id]&orderReward=[orderReward] 0.2秒制限 | |
部下挿げ替え http://game.coderunner.jp/change?token=[your_token]&worker=[worker_id],[worker_id],[worker_id],... 0.2秒制限 | |
http://game.coderunner.jp/changeJson?token=[your_token]&worker=[worker_id],[worker_id],[worker_id],... | |
情報取得系 会社情報取得 http://game.coderunner.jp/getinfo?token=[your_token] 1秒制限 | |
http://game.coderunner.jp/getinfoJson?token=[your_token] | |
外注情報取得 http://game.coderunner.jp/getout?token=[your_token] 1秒制限 | |
http://game.coderunner.jp/getoutJson?token=[your_token] | |
コンテスト系 コメント変更 http://game.coderunner.jp/comment?token=[your_token]&text=hello! 0.2秒制限 | |
質問フォーラム http://game.coderunner.jp/clar?token=[your_token] 2秒キャッシュ | |
質問の送信 http://game.coderunner.jp/sendClar?token=[your_token]&text=question 10秒制限 | |
プログラム更新 http://game.coderunner.jp/deploy?token=[your_token] | |
*/ | |
struct task { | |
int id; | |
int time; | |
int load; | |
int pattern; | |
int reward; | |
int risk; | |
int orderReward; | |
}; | |
ostream &operator<<(ostream &os, const task &t) | |
{ | |
os << "id:" << t.id << " time:" << t.time << " load:" << t.load << " pat:" << t.pattern << " reward:" << t.reward << " risk:" << t.risk << " orew:" << t.orderReward << endl; | |
return os; | |
} | |
task get_task(istream &is) | |
{ | |
task ret; | |
is >> ret.id >> ret.time >> ret.load >> ret.pattern >> ret.reward >> ret.risk; | |
return ret; | |
} | |
task get_out2(istream &is) | |
{ | |
task ret; | |
is >> ret.id >> ret.time >> ret.load >> ret.pattern >> ret.reward >> ret.risk >> ret.orderReward; | |
ret.risk = 0; | |
return ret; | |
} | |
task get_out(istream &is) | |
{ | |
task ret; | |
is >> ret.id >> ret.time >> ret.load >> ret.pattern >> ret.reward; | |
ret.risk = 0; | |
return ret; | |
} | |
struct worker { | |
int id; | |
vector<int> speed; | |
int time; | |
int exp; | |
}; | |
bool is_strong(const worker &w) | |
{ | |
int strong = 0, best = 0; | |
for (auto &s: w.speed) { | |
if (s >= 8) | |
strong++; | |
if (s >= 9) | |
best++; | |
} | |
return (strong >= 3 || best >= 2); | |
} | |
ostream &operator<<(ostream &os, const worker &w) | |
{ | |
os << w.id << ":"; | |
for (auto &s: w.speed) os << " " << s; | |
os << ": " << w.time << ": " << w.exp; | |
if (is_strong(w)) os << " *"; | |
os << endl; | |
return os; | |
} | |
worker get_worker(istream &is) | |
{ | |
worker ret; | |
is >> ret.id; | |
for (int i = 0; i < 50; i++) { | |
int s; | |
is >> s; | |
ret.speed.push_back(s); | |
} | |
is >> ret.time >> ret.exp; | |
return ret; | |
} | |
struct company_info { | |
int score; | |
vector<worker> workers; | |
vector<task> tasks; | |
vector<task> outsources; | |
}; | |
ostream &operator<<(ostream &os, const company_info &info) | |
{ | |
os << "score: " << info.score << endl | |
<< "worker: " << endl; | |
for (auto &w: info.workers) | |
os << w; | |
os << "task: " << endl; | |
for (auto &t: info.tasks) | |
os << t; | |
os << "outsource: " << endl; | |
for (auto &t: info.outsources) | |
os << t; | |
return os; | |
} | |
company_info get_company_info(istream &is) | |
{ | |
company_info ret; | |
is >> ret.score; | |
int num_worker, num_type; | |
is >> num_worker >> num_type; | |
assert(num_type == 50); | |
for (int i = 0; i < num_worker; i++) { | |
ret.workers.emplace_back(get_worker(is)); | |
} | |
int num_task; | |
is >> num_task; | |
for (int i = 0; i < num_task; i++) { | |
ret.tasks.emplace_back(get_task(is)); | |
} | |
int num_out; | |
is >> num_out; | |
for (int i = 0; i < num_out; i++) { | |
ret.outsources.emplace_back(get_out(is)); | |
} | |
return ret; | |
} | |
string api(const string &url) | |
{ | |
ostringstream cmd; | |
cmd << "curl -s \"" << url << "token=" << token << "\" > tmp.txt"; | |
cout << cmd.str() << endl; | |
auto rc = system(cmd.str().c_str()); | |
if (rc != 0) { | |
cout << "fail to get: " << url << endl; | |
abort(); | |
} | |
ifstream ifs("tmp.txt" , ios::binary); | |
istreambuf_iterator<char> is(ifs); | |
return string(is, istreambuf_iterator<char>()); | |
} | |
company_info api_company_info() | |
{ | |
istringstream iss(api("http://open-game.coderunner.jp/getinfo?")); | |
return get_company_info(iss); | |
} | |
task api_taketask() | |
{ | |
istringstream iss(api("http://open-game.coderunner.jp/taketask?")); | |
return get_task(iss); | |
} | |
void api_change(const vector<int> &workers) | |
{ | |
ostringstream oss; | |
oss << "http://open-game.coderunner.jp/change?worker="; | |
for (int i = 0; i < (int)workers.size(); i++) { | |
if (i != 0) oss << ","; | |
oss << workers[i]; | |
} | |
oss << "&"; | |
api(oss.str()); | |
return; | |
} | |
void api_assign(int task_id, const vector<int> &workers) | |
{ | |
ostringstream oss; | |
oss << "http://open-game.coderunner.jp/assign?task=" << task_id | |
<< "&worker="; | |
for (int i = 0; i < (int)workers.size(); i++) { | |
if (i != 0) oss << ","; | |
oss << workers[i]; | |
} | |
oss << "&"; | |
api(oss.str()); | |
} | |
void api_outsource(int task_id, int reward) | |
{ | |
ostringstream oss; | |
oss << "http://open-game.coderunner.jp/outsource?task=" << task_id << "&orderReward=" << reward << "&"; | |
api(oss.str()); | |
} | |
vector<task> api_getout() | |
{ | |
istringstream iss(api("http://open-game.coderunner.jp/getout?")); | |
int n; iss >> n; | |
vector<task> ret; | |
for (int i = 0; i < n; i++) | |
ret.emplace_back(get_out(iss)); | |
return ret; | |
} | |
int main() | |
{ | |
/* | |
// Shain Gacha | |
if (0) { | |
vector<int> ws; | |
for (int i = 0; i < 50; i++) | |
ws.push_back(i); | |
api_change(ws); | |
} | |
for (;;) { | |
auto cinfo = api_company_info(); | |
cout << cinfo << endl; | |
sleep(1); | |
vector<int> weeks; | |
for (auto &w: cinfo.workers) { | |
if (!is_strong(w)) | |
weeks.push_back(w.id); | |
} | |
if (weeks.size() == 0) { | |
cout << "completed!" << endl; | |
break; | |
} | |
cout << "weeks: " << weeks.size() << endl; | |
for (auto &w: weeks) cout << w << ", "; | |
cout << endl; | |
api_change(weeks); | |
} | |
*/ | |
/* | |
while (true) { | |
auto os = api_getout(); | |
sleep(1); | |
cout << os.size() << ": "; | |
for (auto &o: os) | |
cout << "* " << o << endl; | |
} | |
*/ | |
while (true) { | |
auto cinfo = api_company_info(); | |
sleep(1); | |
vector<int> avail; | |
for (int i = 0; i < (int)cinfo.workers.size(); i++) | |
if (cinfo.workers[i].time == 0) | |
avail.push_back(i); | |
/* | |
if (avail.size() < 10) { | |
sleep(1); | |
continue; | |
} | |
*/ | |
// if there are no tasks | |
if (cinfo.tasks.empty()) { | |
auto t = api_taketask(); | |
cout << "*** " << t << endl; | |
continue; | |
} | |
cout << cinfo << endl; | |
cout << "score: " << cinfo.score << endl; | |
cout << "avail: " << avail.size() << "/ 50" << endl; | |
auto &t = cinfo.tasks[0]; | |
sort(avail.begin(), avail.end(), [&](int i, int j) { | |
return cinfo.workers[i].speed[t.pattern] > cinfo.workers[j].speed[t.pattern]; | |
}); | |
vector<int> use; | |
int sum = 0; | |
for (int i = 0; i < (int)avail.size(); i++) { | |
auto ix = avail[i]; | |
auto sp = cinfo.workers[ix].speed[t.pattern]; | |
if (sp <= 5) continue; | |
use.push_back(ix); | |
sum += sp; | |
// if (t.time * sum >= t.load) | |
// break; | |
} | |
// cout << t.time << ", " << sum << ", " << t.load << endl; | |
if (t.time * sum < t.load) { | |
// gaichuu | |
api_outsource(t.id, t.risk); | |
sleep(5); | |
continue; | |
} | |
api_assign(t.id, use); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment