Last active
November 18, 2015 06:06
-
-
Save rendon/edf392a028c1fe1b66fd to your computer and use it in GitHub Desktop.
TimeTravellingSalesman.cpp
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 <algorithm> | |
#include <sstream> | |
#include <vector> | |
#include <string> | |
#include <fstream> | |
#include <set> | |
#include <numeric> | |
#include <regex> | |
using namespace std; | |
class TimeTravellingSalesman { | |
public: | |
vector<string> split(string str, string pattern) { | |
regex r(pattern); | |
sregex_token_iterator it(begin(str), end(str), r, -1); | |
sregex_token_iterator end; | |
vector<string> tokens; | |
for ( ; it != end; ++it) { | |
tokens.push_back(it->str()); | |
} | |
return tokens; | |
} | |
long long determineCost(int N, vector<string> roads) { | |
string input; | |
input = accumulate(begin(roads), end(roads), input); | |
auto edges = split(input, "\\s+"); | |
cout << endl; | |
for (string e : edges) { | |
auto road = split(e, ","); | |
int a = atoi(road[0].c_str()); | |
int b = atoi(road[1].c_str()); | |
int c = atoi(road[2].c_str()); | |
cout << a << " " << b << " : " << c << "\n"; | |
} | |
return 0; | |
} | |
}; | |
// region Test | |
// CUT begin | |
const string COLOR_OFF = "\e[0m"; | |
const string GREEN = "\e[0;32m"; | |
const string RED = "\e[0;31m"; | |
const string BOLD_WHITE = "\e[1;37m"; | |
ifstream data(".TimeTravellingSalesman.sample"); | |
string next_line() { | |
string s; | |
getline(data, s); | |
return s; | |
} | |
template <typename T> void from_stream(T &t) { | |
stringstream ss(next_line()); | |
ss >> t; | |
} | |
void from_stream(string &s) { | |
s = next_line(); | |
} | |
template <typename T> void from_stream(vector<T> &ts) { | |
int len; | |
from_stream(len); | |
ts.clear(); | |
for (int i = 0; i < len; ++i) { | |
T t; | |
from_stream(t); | |
ts.push_back(t); | |
} | |
} | |
template <typename T> | |
string to_string(T t) { | |
stringstream s; | |
s << t; | |
return s.str(); | |
} | |
string to_string(string t) { | |
return "\"" + t + "\""; | |
} | |
bool do_test(int N, vector<string> roads, long long __expected) { | |
time_t startClock = clock(); | |
TimeTravellingSalesman *instance = new TimeTravellingSalesman(); | |
long long __result = instance->determineCost(N, roads); | |
double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC; | |
delete instance; | |
if (__result == __expected) { | |
cout << GREEN << "PASSED!" << COLOR_OFF << " (" << elapsed << " seconds)" << endl; | |
cout << "------------------------------------------------------------" << endl; | |
return true; | |
} | |
else { | |
cout << RED << "FAILED!" << COLOR_OFF << " (" << elapsed << " seconds)" << endl; | |
cout << " Expected: " << to_string(__expected) << endl; | |
cout << " Received: " << to_string(__result) << endl; | |
cout << "------------------------------------------------------------" << endl; | |
return false; | |
} | |
} | |
int run_test(bool mainProcess, const set<int> &case_set, const string command) { | |
int cases = 0, passed = 0; | |
while (true) { | |
if (next_line().find("--") != 0) | |
break; | |
int N; | |
from_stream(N); | |
vector<string> roads; | |
from_stream(roads); | |
next_line(); | |
long long __answer; | |
from_stream(__answer); | |
cases++; | |
if (case_set.size() > 0 && case_set.find(cases - 1) == case_set.end()) | |
continue; | |
cout << BOLD_WHITE << " Testcase #" << cases - 1 << COLOR_OFF << " ... "; | |
if ( do_test(N, roads, __answer)) { | |
passed++; | |
} | |
} | |
if (mainProcess) { | |
cout << GREEN << "PASS: " << passed; | |
if (passed < cases) | |
cout << RED << "\tFAIL: " << cases - passed; | |
cout << COLOR_OFF << endl; | |
int T = time(NULL) - 1438962127; | |
double PT = T / 60.0, TT = 75.0; | |
cout << "Time : " << T / 60 << " minutes " << T % 60 << " secs" << endl; | |
cout << "Score : " << 1000 * (0.3 + (0.7 * TT * TT) / (10.0 * PT * PT + TT * TT)) << " points" << endl; | |
} | |
return 0; | |
} | |
int main(int argc, char *argv[]) { | |
cout.setf(ios::fixed, ios::floatfield); | |
cout.precision(2); | |
set<int> cases; | |
bool mainProcess = true; | |
for (int i = 1; i < argc; ++i) { | |
if ( string(argv[i]) == "-") { | |
mainProcess = false; | |
} else { | |
cases.insert(atoi(argv[i])); | |
} | |
} | |
if (mainProcess) { | |
cout << "TimeTravellingSalesman (1000 Points)" << endl << endl; | |
} | |
return run_test(mainProcess, cases, argv[0]); | |
} | |
// CUT end | |
// endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment