|
|
@ -2,7 +2,8 @@ |
|
|
|
#include <cpr/cpr.h> |
|
|
|
#include <cpr/cpr.h> |
|
|
|
#include "days/days.h" |
|
|
|
#include "days/days.h" |
|
|
|
|
|
|
|
|
|
|
|
Input getInput(int day, string key, bool useTestInput, int testFetchIndex) { |
|
|
|
|
|
|
|
|
|
|
|
Input getInput(int day, string key, bool useTestInput) { |
|
|
|
Input input; |
|
|
|
Input input; |
|
|
|
|
|
|
|
|
|
|
|
char dayStrPadded[3]; |
|
|
|
char dayStrPadded[3]; |
|
|
@ -11,30 +12,18 @@ Input getInput(int day, string key, bool useTestInput, int testFetchIndex) { |
|
|
|
string localFilePath = "input/" + string(dayStrPadded) + postfix; |
|
|
|
string localFilePath = "input/" + string(dayStrPadded) + postfix; |
|
|
|
|
|
|
|
|
|
|
|
if (!std::filesystem::exists({localFilePath})) { |
|
|
|
if (!std::filesystem::exists({localFilePath})) { |
|
|
|
|
|
|
|
if (key.empty()) |
|
|
|
|
|
|
|
return input; |
|
|
|
string dayStr = std::to_string(day); |
|
|
|
string dayStr = std::to_string(day); |
|
|
|
string url = "https://adventofcode.com/2022/day/" + dayStr; |
|
|
|
string url = "https://adventofcode.com/2022/day/" + dayStr + "/input"; |
|
|
|
auto cookies = cpr::Cookies{{"session", key}}; |
|
|
|
cout << "Input does not exist. Fetching from " + url << endl; |
|
|
|
if (!useTestInput){ |
|
|
|
std::ofstream file(localFilePath); |
|
|
|
if (key.empty()){ |
|
|
|
file << cpr::Get(cpr::Url{url}, cpr::Cookies{{"session", key}}).text; |
|
|
|
cout << "Session key Cookie is missing, cannot fetch Input" << endl; |
|
|
|
file.close(); |
|
|
|
return input; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
url += "/input"; |
|
|
|
|
|
|
|
cout << "Input does not exist. Fetching from " + url << endl; |
|
|
|
|
|
|
|
std::ofstream file(localFilePath); |
|
|
|
|
|
|
|
file << cpr::Get(cpr::Url{url}, cookies).text; |
|
|
|
|
|
|
|
file.close(); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
cout << "Test Input does not exist. Fetching from " + url << " from <code>-Tag with index " << testFetchIndex << endl; |
|
|
|
|
|
|
|
std::ofstream file(localFilePath); |
|
|
|
|
|
|
|
string res = cpr::Get(cpr::Url{url}).text; |
|
|
|
|
|
|
|
size_t i1 = findAll(res, "<code>")[testFetchIndex]; |
|
|
|
|
|
|
|
size_t i2 = findAll(res, "</code>")[testFetchIndex]; |
|
|
|
|
|
|
|
file << res.substr(i1 + 6, i2 - (i1 + 6)); |
|
|
|
|
|
|
|
file.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO fetch testInput from first <code>-Tag in HTML
|
|
|
|
|
|
|
|
|
|
|
|
std::ifstream file(localFilePath); |
|
|
|
std::ifstream file(localFilePath); |
|
|
|
|
|
|
|
|
|
|
|
string line; |
|
|
|
string line; |
|
|
@ -59,16 +48,19 @@ string getSessionKey() { |
|
|
|
int main(int argc, char *argv[]) { |
|
|
|
int main(int argc, char *argv[]) { |
|
|
|
|
|
|
|
|
|
|
|
auto key = getSessionKey(); |
|
|
|
auto key = getSessionKey(); |
|
|
|
|
|
|
|
bool useTestInput = false; |
|
|
|
auto days = getAllDays(); |
|
|
|
auto days = getAllDays(); |
|
|
|
std::filesystem::create_directory("input"); |
|
|
|
std::filesystem::create_directory("input"); |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; i++) { |
|
|
|
int i = 1; |
|
|
|
string arg = argv[i]; |
|
|
|
if (argc > 1 && string(argv[1]) == "T") { |
|
|
|
bool useTestInput = arg.size() > 1 && arg[1] == 'T'; |
|
|
|
useTestInput = true; |
|
|
|
int testFetchIndex = useTestInput && arg.size() > 2 ? arg[2] - '0' : 0; |
|
|
|
i++; |
|
|
|
int dayNum = std::stoi(arg); |
|
|
|
} |
|
|
|
|
|
|
|
for (; i < argc; i++) { |
|
|
|
|
|
|
|
int dayNum = std::stoi(argv[i]); |
|
|
|
cout << "Running day " << dayNum << endl; |
|
|
|
cout << "Running day " << dayNum << endl; |
|
|
|
Input input = getInput(dayNum, key, useTestInput, testFetchIndex); |
|
|
|
Input input = getInput(dayNum, key, useTestInput); |
|
|
|
Day *day = days[dayNum - 1]; |
|
|
|
Day *day = days[dayNum - 1]; |
|
|
|
int code = day->run(input); |
|
|
|
int code = day->run(input); |
|
|
|
if (code != 0) |
|
|
|
if (code != 0) |
|
|
|