From 16b77ae41b395fe317cfad9dfa0c9012c17941db Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Thu, 8 Dec 2022 20:34:46 +0100 Subject: [PATCH] Better test input support --- README.md | 21 ++++++++++++++++++++- src/main.cpp | 33 +++++++++++++++------------------ src/util.h | 16 +++++++++++++++- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 2b85b62..1fbbfdb 100644 --- a/README.md +++ b/README.md @@ -11,4 +11,23 @@ Expects runtime arguments as days, for example: "5 9 1" executes Days 5, 9, 1 in this order. -Add argument "T" before to use XX_testInput.txt rather than XX_input.txt \ No newline at end of file +#### Test Inputs + +Add argument "T" after each Day to use XX_testInput.txt rather than XX_input.txt + +For Test-Inputs, it tries to fetch the content of the first code-Tag. You can control this by adding the index of the desired code-Tag. +Example: + +"3T 6T1 9" + +- Day 3 with content from inside the first code-Tag +- Day 6 with content from inside the second code-Tag +- Day 9 with regular input + +#### Get index with Browser Dev Tools: + +Select the correct code-Tag in the inspector, then paste this into the console: + + +Array.from(document.getElementsByTagName("code")).indexOf($0) + diff --git a/src/main.cpp b/src/main.cpp index c85f209..c835d9b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,8 +2,7 @@ #include #include "days/days.h" - -Input getInput(int day, string key, bool useTestInput) { +Input getInput(int day, string key, bool useTestInput, int testFetchIndex) { Input input; char dayStrPadded[3]; @@ -11,26 +10,27 @@ Input getInput(int day, string key, bool useTestInput) { string postfix = useTestInput ? "_testInput.txt" : "_input.txt"; string localFilePath = "input/" + string(dayStrPadded) + postfix; - std::filesystem::remove(localFilePath); - if (!std::filesystem::exists({localFilePath})) { string dayStr = std::to_string(day); string url = "https://adventofcode.com/2022/day/" + dayStr; auto cookies = cpr::Cookies{{"session", key}}; if (!useTestInput){ - if (key.empty()) + if (key.empty()){ + cout << "Session key Cookie is missing, cannot fetch Input" << endl; 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 { - // TODO fetch testInput from first -Tag in HTML + cout << "Test Input does not exist. Fetching from " + url << " from -Tag with index " << testFetchIndex << endl; std::ofstream file(localFilePath); - auto header = cpr::Header{{"User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:107.0) Gecko/20100101 Firefox/107.0"}, - {"Connection", "keep-alive"}}; - file << cpr::Get(cpr::Url{url}, cookies, header).text; + string res = cpr::Get(cpr::Url{url}).text; + size_t i1 = findAll(res, "")[testFetchIndex]; + size_t i2 = findAll(res, "")[testFetchIndex]; + file << res.substr(i1 + 6, i2 - (i1 + 6)); file.close(); } } @@ -59,19 +59,16 @@ string getSessionKey() { int main(int argc, char *argv[]) { auto key = getSessionKey(); - bool useTestInput = false; auto days = getAllDays(); std::filesystem::create_directory("input"); - int i = 1; - if (argc > 1 && string(argv[1]) == "T") { - useTestInput = true; - i++; - } - for (; i < argc; i++) { - int dayNum = std::stoi(argv[i]); + for (int i = 1; i < argc; i++) { + string arg = argv[i]; + bool useTestInput = arg.size() > 1 && arg[1] == 'T'; + int testFetchIndex = useTestInput && arg.size() > 2 ? arg[2] - '0' : 0; + int dayNum = std::stoi(arg); cout << "Running day " << dayNum << endl; - Input input = getInput(dayNum, key, useTestInput); + Input input = getInput(dayNum, key, useTestInput, testFetchIndex); Day *day = days[dayNum - 1]; int code = day->run(input); if (code != 0) diff --git a/src/util.h b/src/util.h index 571d82e..35a400d 100644 --- a/src/util.h +++ b/src/util.h @@ -10,4 +10,18 @@ using std::stoi, std::to_string; using std::cout, std::endl; -using std::string, std::vector, std::set, std::pair, std::map; \ No newline at end of file +using std::string, std::vector, std::set, std::pair, std::map; + +inline vector findAll(const string& data, const string& toSearch){ + vector indices; + + size_t pos = data.find(toSearch); + + while (pos != string::npos){ + indices.push_back(pos); + + pos = data.find(toSearch, pos + toSearch.size()); + } + + return indices; +} \ No newline at end of file