better setup

master
Benjamin Kraft 3 years ago
parent 51bdd316a1
commit db9d8ff593
  1. 3
      input/01_testInput.txt
  2. 1000
      input/02_input.txt
  3. 8
      src/01/Day01.cpp
  4. 6
      src/01/Day01.h
  5. 8
      src/02/Day02.cpp
  6. 6
      src/02/Day02.h
  7. 8
      src/03/Day03.cpp
  8. 6
      src/03/Day03.h
  9. 33
      src/Day.h
  10. 23
      src/main.cpp

@ -0,0 +1,3 @@
hallo
du
da

File diff suppressed because it is too large Load Diff

@ -1,6 +1,10 @@
#include "Day01.h"
int Day01::run() {
return 0;
Result Day01::Task1() {
return Day::Task1();
}
Result Day01::Task2() {
return Day::Task2();
}

@ -5,9 +5,11 @@
#include "../Day.h"
class Day01 : public Day {
protected:
Result Task1() override;
Result Task2() override;
public:
explicit Day01(std::vector<std::string> &input) : Day(input) {}
int run() override;
explicit Day01(Input &input) : Day(input) {}
};

@ -1,6 +1,10 @@
#include "Day02.h"
int Day02::run() {
return 0;
Result Day02::Task1() {
return Day::Task1();
}
Result Day02::Task2() {
return Day::Task2();
}

@ -5,9 +5,11 @@
#include "../Day.h"
class Day02 : public Day {
protected:
Result Task1() override;
Result Task2() override;
public:
explicit Day02(std::vector<std::string> &input) : Day(input) {}
int run() override;
explicit Day02(Input &input) : Day(input) {}
};

@ -1,6 +1,10 @@
#include "Day03.h"
int Day03::run() {
return 0;
Result Day03::Task1() {
return Day::Task1();
}
Result Day03::Task2() {
return Day::Task2();
}

@ -5,9 +5,11 @@
#include "../Day.h"
class Day03 : public Day {
protected:
Result Task1() override;
Result Task2() override;
public:
explicit Day03(std::vector<std::string> &input) : Day(input) {}
int run() override;
explicit Day03(Input &input) : Day(input) {}
};

@ -3,15 +3,38 @@
#include <vector>
#include <string>
#include <tuple>
#include <chrono>
#include <iostream>
class Day {
using namespace std::chrono;
typedef std::string Result;
typedef std::vector<std::string> Input;
class Day {
protected:
std::vector<std::string> input;
Input input;
virtual Result Task1() { return ""; }
virtual Result Task2() { return ""; }
public:
explicit Day(std::vector<std::string> &input) : input(input) {}
virtual int run() {
explicit Day(Input &input) : input(input) {}
int run() {
auto start = high_resolution_clock::now();
std::cout << "Task 1" << std::endl;
Result result1 = Task1();
auto stop1 = high_resolution_clock::now();
auto dur1 = duration_cast<milliseconds>(stop1 - start);
std::cout << "Result: " << result1 << std::endl;
std::cout << "Duration: " << dur1.count() << "ms" << std::endl;
std::cout << "Task 2" << std::endl;
Result result2 = Task2();
auto stop2 = high_resolution_clock::now();
auto dur2 = duration_cast<milliseconds>(stop2 - stop1);
std::cout << "Result: " << result2 << std::endl;
std::cout << "Duration: " << dur2.count() << "ms" << std::endl << std::endl;
return 0;
}
};

@ -1,16 +1,16 @@
#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
#include "days.h"
#include <cpr/cpr.h>
std::vector<std::string> getInput(int day, std::string key){
using namespace std::chrono;
std::vector<std::string> getInput(int day, std::string key, bool useTestInput){
std::vector<std::string> result;
char dayStrPadded[3];
sprintf(dayStrPadded, "%02u", day);
std::string localFilePath = "input/" + std::string(dayStrPadded) + "_input.txt";
std::string postfix = useTestInput ? "_testInput.txt" : "_input.txt";
std::string localFilePath = "input/" + std::string(dayStrPadded) + postfix;
if (!std::filesystem::exists({localFilePath})){
if (key.empty())
@ -18,10 +18,8 @@ std::vector<std::string> getInput(int day, std::string key){
std::string dayStr = std::to_string(day);
std::string url = "https://adventofcode.com/2021/day/" + dayStr + "/input";
std::cout << "Input does not exist. Fetching from " + url << std::endl;
cpr::Response res = cpr::Get(cpr::Url {url}, cpr::Cookies{{"session", key}});
std::string content = res.text;
std::ofstream file(localFilePath);
file << content;
file << cpr::Get(cpr::Url {url}, cpr::Cookies{{"session", key}}).text;
file.close();
}
@ -49,11 +47,18 @@ std::string getSessionKey(){
int main(int argc, char *argv[]) {
std::string key = getSessionKey();
bool useTestInput = false;
for (int i = 1; i < argc; i++){
int dayNum = std::stoi(argv[i]);
std::vector<std::string> input = getInput(dayNum, key);
std::cout << "Running day " << dayNum << std::endl;
auto input = getInput(dayNum, key, useTestInput);
auto day = getDay(dayNum, input);
if (day == nullptr){
std::cout << "Could not run day " << dayNum << std::endl;
continue;
}
int code = day->run();
if (code != 0)
return code;

Loading…
Cancel
Save