|
|
|
@ -3,20 +3,20 @@ |
|
|
|
|
#include "days/days.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Input getInput(int day, string key, bool useTestInput) { |
|
|
|
|
Input getInput(int day, std::string key, bool useTestInput) { |
|
|
|
|
Input input; |
|
|
|
|
|
|
|
|
|
char dayStrPadded[3]; |
|
|
|
|
sprintf(dayStrPadded, "%02u", day); |
|
|
|
|
string postfix = useTestInput ? "_testInput.txt" : "_input.txt"; |
|
|
|
|
string localFilePath = "input/" + string(dayStrPadded) + postfix; |
|
|
|
|
std::string postfix = useTestInput ? "_testInput.txt" : "_input.txt"; |
|
|
|
|
std::string localFilePath = "input/" + std::string(dayStrPadded) + postfix; |
|
|
|
|
|
|
|
|
|
if (!std::filesystem::exists({localFilePath})) { |
|
|
|
|
if (key.empty()) |
|
|
|
|
return input; |
|
|
|
|
string dayStr = std::to_string(day); |
|
|
|
|
string url = "https://adventofcode.com/2022/day/" + dayStr + "/input"; |
|
|
|
|
cout << "Input does not exist. Fetching from " + url << endl; |
|
|
|
|
std::string dayStr = std::to_string(day); |
|
|
|
|
std::string url = "https://adventofcode.com/2022/day/" + dayStr + "/input"; |
|
|
|
|
std::cout << "Input does not exist. Fetching from " + url << std::endl; |
|
|
|
|
std::ofstream file(localFilePath); |
|
|
|
|
file << cpr::Get(cpr::Url{url}, cpr::Cookies{{"session", key}}).text; |
|
|
|
|
file.close(); |
|
|
|
@ -24,7 +24,7 @@ Input getInput(int day, string key, bool useTestInput) { |
|
|
|
|
|
|
|
|
|
std::ifstream file(localFilePath); |
|
|
|
|
|
|
|
|
|
string line; |
|
|
|
|
std::string line; |
|
|
|
|
while (std::getline(file, line)) |
|
|
|
|
input.push_back(line); |
|
|
|
|
|
|
|
|
@ -33,8 +33,8 @@ Input getInput(int day, string key, bool useTestInput) { |
|
|
|
|
return input; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string getSessionKey() { |
|
|
|
|
string key; |
|
|
|
|
std::string getSessionKey() { |
|
|
|
|
std::string key; |
|
|
|
|
std::ifstream file("session"); |
|
|
|
|
if (!file.good()) |
|
|
|
|
return ""; |
|
|
|
@ -51,13 +51,13 @@ int main(int argc, char *argv[]) { |
|
|
|
|
std::filesystem::create_directory("input"); |
|
|
|
|
|
|
|
|
|
int i = 1; |
|
|
|
|
if (argc > 1 && string(argv[1]) == "T") { |
|
|
|
|
if (argc > 1 && std::string(argv[1]) == "T") { |
|
|
|
|
useTestInput = true; |
|
|
|
|
i++; |
|
|
|
|
} |
|
|
|
|
for (; i < argc; i++) { |
|
|
|
|
int dayNum = std::stoi(argv[i]); |
|
|
|
|
cout << "Running day " << dayNum << endl; |
|
|
|
|
std::cout << "Running day " << dayNum << std::endl; |
|
|
|
|
Input input = getInput(dayNum, key, useTestInput); |
|
|
|
|
Day *day = days[dayNum - 1]; |
|
|
|
|
int code = day->run(input); |
|
|
|
|