added fetching from site if input not present locally

master
Benjamin Kraft 3 years ago
parent 96e9222a52
commit b8b4494628
  1. 1
      .gitignore
  2. 11
      CMakeLists.txt
  3. 2252
      input/01_input.txt
  4. 1000
      input/02_input.txt
  5. 1000
      input/03_input.txt
  6. 2
      src/02/Day02.cpp
  7. 2
      src/03/Day03.cpp
  8. 37
      src/main.cpp

1
.gitignore vendored

@ -1 +1,2 @@
/cmake-build-debug/ /cmake-build-debug/
session/

@ -3,11 +3,16 @@ project(AdventOfCode2022)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
include(FetchContent) if (WIN32)
FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/libcpr/cpr.git GIT_TAG 100cf2050be7619430a615cd0d580b33c62fde6b) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
FetchContent_MakeAvailable(cpr) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
endif()
file(GLOB_RECURSE SRC_FILES src/**.cpp src/**.h) file(GLOB_RECURSE SRC_FILES src/**.cpp src/**.h)
add_executable(AdventOfCode2022 ${SRC_FILES}) add_executable(AdventOfCode2022 ${SRC_FILES})
include(FetchContent)
FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/libcpr/cpr.git GIT_TAG 100cf2050be7619430a615cd0d580b33c62fde6b)
FetchContent_MakeAvailable(cpr)
target_link_libraries(AdventOfCode2022 PRIVATE cpr::cpr) target_link_libraries(AdventOfCode2022 PRIVATE cpr::cpr)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -2,5 +2,5 @@
#include "Day02.h" #include "Day02.h"
int Day02::run() { int Day02::run() {
return 1; return 0;
} }

@ -2,5 +2,5 @@
#include "Day03.h" #include "Day03.h"
int Day03::run() { int Day03::run() {
return 1; return 0;
} }

@ -1,43 +1,58 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <fstream>
#include <vector> #include <vector>
#include <filesystem> #include <filesystem>
#include "days.h" #include "days.h"
//#include <cpr/cpr.h> #include <cpr/cpr.h>
std::vector<std::string> getInput(int day){ std::vector<std::string> getInput(int day, std::string key){
std::vector<std::string> result;
char dayStrPadded[3]; char dayStrPadded[3];
sprintf(dayStrPadded, "%02u", day); sprintf(dayStrPadded, "%02u", day);
std::string localFilePath = "input/" + std::string(dayStrPadded) + "_input.txt"; std::string localFilePath = "input/" + std::string(dayStrPadded) + "_input.txt";
if (!std::filesystem::exists({localFilePath})){ if (!std::filesystem::exists({localFilePath})){
if (key.empty())
return result;
std::string dayStr = std::to_string(day); std::string dayStr = std::to_string(day);
std::string url = "https://adventofcode.com/2021/day/" + dayStr + "/input"; std::string url = "https://adventofcode.com/2021/day/" + dayStr + "/input";
std::string key = "sdfsd";
std::string cookie = "session=" + key;
std::cout << "Input does not exist. Fetching from " + url << std::endl; std::cout << "Input does not exist. Fetching from " + url << std::endl;
//cpr::Response res = cpr::Get(cpr::Url {url}, cpr::Header{{"Cookie", cookie}}); cpr::Response res = cpr::Get(cpr::Url {url}, cpr::Cookies{{"session", key}});
//std::cout << res.text << std::endl; std::string content = res.text;
std::ofstream file(localFilePath);
file << content;
file.close();
} }
std::fstream file; std::ifstream file(localFilePath);
file.open(localFilePath, std::ios::in);
std::vector<std::string> result;
std::string line; std::string line;
while (std::getline(file, line)) while (std::getline(file, line))
result.push_back(line); result.push_back(line);
file.close();
return result; return result;
} }
std::string getSessionKey(){
std::string key;
std::ifstream file("session");
if (!file.good())
return "";
file >> key;
file.close();
return key;
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
std::string key = getSessionKey();
for (int i = 1; i < argc; i++){ for (int i = 1; i < argc; i++){
int dayNum = std::stoi(argv[i]); int dayNum = std::stoi(argv[i]);
std::vector<std::string> input = getInput(dayNum); std::vector<std::string> input = getInput(dayNum, key);
auto day = getDay(dayNum, input); auto day = getDay(dayNum, input);
int code = day->run(); int code = day->run();
if (code != 0) if (code != 0)

Loading…
Cancel
Save