Compare commits

...

2 Commits

  1. 16
      src/Day.h
  2. 4
      src/days/01/Day01.cpp
  3. 10
      src/days/02/Day02.cpp
  4. 1
      src/days/02/Day02.h
  5. 22
      src/main.cpp

@ -4,8 +4,8 @@
#include "util.h" #include "util.h"
using namespace std::chrono; using namespace std::chrono;
typedef std::string Result; typedef string Result;
typedef std::vector<std::string> Input; typedef std::vector<string> Input;
class Day { class Day {
protected: protected:
@ -21,19 +21,19 @@ public:
auto start = high_resolution_clock::now(); auto start = high_resolution_clock::now();
std::cout << "Task 1" << std::endl; cout << "Task 1" << endl;
Result result1 = Task1(); Result result1 = Task1();
auto stop1 = high_resolution_clock::now(); auto stop1 = high_resolution_clock::now();
auto dur1 = duration_cast<microseconds>(stop1 - start); auto dur1 = duration_cast<microseconds>(stop1 - start);
std::cout << "Result: " << result1 << std::endl; cout << "Result: " << result1 << endl;
std::cout << "Duration: " << double(dur1.count()) / 1000. << "ms" << std::endl; cout << "Duration: " << double(dur1.count()) / 1000. << "ms" << endl;
std::cout << "Task 2" << std::endl; cout << "Task 2" << endl;
Result result2 = Task2(); Result result2 = Task2();
auto stop2 = high_resolution_clock::now(); auto stop2 = high_resolution_clock::now();
auto dur2 = duration_cast<microseconds>(stop2 - stop1); auto dur2 = duration_cast<microseconds>(stop2 - stop1);
std::cout << "Result: " << result2 << std::endl; cout << "Result: " << result2 << endl;
std::cout << "Duration: " << double(dur2.count()) / 1000. << "ms" << std::endl << std::endl; cout << "Duration: " << double(dur2.count()) / 1000. << "ms" << endl << endl;
return 0; return 0;
} }

@ -2,7 +2,7 @@
Result Day01::Task1() { Result Day01::Task1() {
subSums.push_back(0); subSums.push_back(0);
for (const std::string& line : input){ for (const string &line: input) {
if (line.empty()) if (line.empty())
subSums.push_back(0); subSums.push_back(0);
else else
@ -15,7 +15,7 @@ Result Day01::Task2() {
std::sort(subSums.begin(), subSums.end()); std::sort(subSums.begin(), subSums.end());
int sum = 0; int sum = 0;
for (int i = 0; i < 3; i++){ for (int i = 0; i < 3; i++) {
sum += subSums.back(); sum += subSums.back();
subSums.pop_back(); subSums.pop_back();
} }

@ -13,7 +13,7 @@ int Day02::getScore(char opponent, char me) {
Result Day02::Task1() { Result Day02::Task1() {
int score = 0; int score = 0;
for (string line : input){ for (string line: input) {
char predict = line.at(0); char predict = line.at(0);
char answer = char(line.at(2) - 23); char answer = char(line.at(2) - 23);
score += getScore(predict, answer); score += getScore(predict, answer);
@ -23,19 +23,19 @@ Result Day02::Task1() {
Result Day02::Task2() { Result Day02::Task2() {
int score = 0; int score = 0;
for (string line : input){ for (string line: input) {
char predict = line.at(0); char predict = line.at(0);
char outcome = line.at(2); char outcome = line.at(2);
char answer; char answer;
if (outcome == 'X'){ if (outcome == 'X') {
if (predict > 'A') answer = char(predict - 1); if (predict > 'A') answer = char(predict - 1);
else answer = 'C'; else answer = 'C';
} }
if (outcome == 'Y'){ if (outcome == 'Y') {
answer = predict; answer = predict;
} }
if (outcome == 'Z'){ if (outcome == 'Z') {
if (predict < 'C') answer = char(predict + 1); if (predict < 'C') answer = char(predict + 1);
else answer = 'A'; else answer = 'A';
} }

@ -7,6 +7,7 @@ protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
private: private:
// chars A, B, C // chars A, B, C
static int getScore(char, char); static int getScore(char, char);

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

Loading…
Cancel
Save