Compare commits

..

No commits in common. '8fa4e90613811fe3850a8c45cdf81b7a4c61de73' and 'dcee17005f61a1ecf78f8e8cc85f371deeca047d' have entirely different histories.

  1. 16
      src/Day.h
  2. 4
      src/days/01/Day01.cpp
  3. 4
      src/days/01/Day01.h
  4. 68
      src/days/02/Day02.cpp
  5. 9
      src/days/02/Day02.h
  6. 4
      src/days/03/Day03.cpp
  7. 4
      src/days/03/Day03.h
  8. 4
      src/days/04/Day04.cpp
  9. 4
      src/days/04/Day04.h
  10. 4
      src/days/05/Day05.cpp
  11. 4
      src/days/05/Day05.h
  12. 4
      src/days/06/Day06.cpp
  13. 4
      src/days/06/Day06.h
  14. 4
      src/days/07/Day07.cpp
  15. 4
      src/days/07/Day07.h
  16. 4
      src/days/08/Day08.cpp
  17. 4
      src/days/08/Day08.h
  18. 4
      src/days/09/Day09.cpp
  19. 4
      src/days/09/Day09.h
  20. 4
      src/days/10/Day10.cpp
  21. 4
      src/days/10/Day10.h
  22. 4
      src/days/11/Day11.cpp
  23. 4
      src/days/11/Day11.h
  24. 4
      src/days/12/Day12.cpp
  25. 4
      src/days/12/Day12.h
  26. 4
      src/days/13/Day13.cpp
  27. 4
      src/days/13/Day13.h
  28. 4
      src/days/14/Day14.cpp
  29. 4
      src/days/14/Day14.h
  30. 4
      src/days/15/Day15.cpp
  31. 4
      src/days/15/Day15.h
  32. 4
      src/days/16/Day16.cpp
  33. 4
      src/days/16/Day16.h
  34. 4
      src/days/17/Day17.cpp
  35. 4
      src/days/17/Day17.h
  36. 4
      src/days/18/Day18.cpp
  37. 4
      src/days/18/Day18.h
  38. 4
      src/days/19/Day19.cpp
  39. 4
      src/days/19/Day19.h
  40. 4
      src/days/20/Day20.cpp
  41. 4
      src/days/20/Day20.h
  42. 4
      src/days/21/Day21.cpp
  43. 4
      src/days/21/Day21.h
  44. 4
      src/days/22/Day22.cpp
  45. 4
      src/days/22/Day22.h
  46. 4
      src/days/23/Day23.cpp
  47. 4
      src/days/23/Day23.h
  48. 4
      src/days/24/Day24.cpp
  49. 4
      src/days/24/Day24.h
  50. 4
      src/days/25/Day25.cpp
  51. 4
      src/days/25/Day25.h
  52. 30
      src/main.cpp

@ -4,8 +4,8 @@
#include "util.h" #include "util.h"
using namespace std::chrono; using namespace std::chrono;
typedef string Result; typedef std::string Result;
typedef std::vector<string> Input; typedef std::vector<std::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();
cout << "Task 1" << endl; std::cout << "Task 1" << std::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);
cout << "Result: " << result1 << endl; std::cout << "Result: " << result1 << std::endl;
cout << "Duration: " << double(dur1.count()) / 1000. << "ms" << endl; std::cout << "Duration: " << double(dur1.count()) / 1000. << "ms" << std::endl;
cout << "Task 2" << endl; std::cout << "Task 2" << std::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);
cout << "Result: " << result2 << endl; std::cout << "Result: " << result2 << std::endl;
cout << "Duration: " << double(dur2.count()) / 1000. << "ms" << endl << endl; std::cout << "Duration: " << double(dur2.count()) / 1000. << "ms" << std::endl << std::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 string &line: input) { for (const std::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();
} }

@ -4,9 +4,9 @@
class Day01 : public Day { class Day01 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
std::vector<int> subSums; std::vector<int> subSums;
}; };

@ -1,46 +1,46 @@
#include "Day02.h" #include "Day02.h"
int Day02::getScore(char opponent, char me) { int Day02::getScore(char opponent, char me) {
int outcome; int outcome;
if (opponent == me) if (opponent == me)
outcome = 3; outcome = 3;
else if (opponent == me + 1 || opponent == me - 2) else if (opponent == me + 1 || opponent == me - 2)
outcome = 0; outcome = 0;
else else
outcome = 6; outcome = 6;
return outcome + me - 64; return outcome + me - 64;
} }
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);
} }
return to_string(score); return to_string(score);
} }
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';
} }
score += getScore(predict, answer); score += getScore(predict, answer);
} }
return to_string(score); return to_string(score);
} }

@ -4,11 +4,10 @@
class Day02 : public Day { class Day02 : public Day {
protected: 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);
}; };

@ -1,9 +1,9 @@
#include "Day03.h" #include "Day03.h"
Result Day03::Task1() { Result Day03::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day03::Task2() { Result Day03::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day03 : public Day { class Day03 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day04.h" #include "Day04.h"
Result Day04::Task1() { Result Day04::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day04::Task2() { Result Day04::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day04 : public Day { class Day04 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day05.h" #include "Day05.h"
Result Day05::Task1() { Result Day05::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day05::Task2() { Result Day05::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day05 : public Day { class Day05 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day06.h" #include "Day06.h"
Result Day06::Task1() { Result Day06::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day06::Task2() { Result Day06::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day06 : public Day { class Day06 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day07.h" #include "Day07.h"
Result Day07::Task1() { Result Day07::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day07::Task2() { Result Day07::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day07 : public Day { class Day07 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day08.h" #include "Day08.h"
Result Day08::Task1() { Result Day08::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day08::Task2() { Result Day08::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day08 : public Day { class Day08 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day09.h" #include "Day09.h"
Result Day09::Task1() { Result Day09::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day09::Task2() { Result Day09::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day09 : public Day { class Day09 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day10.h" #include "Day10.h"
Result Day10::Task1() { Result Day10::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day10::Task2() { Result Day10::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day10 : public Day { class Day10 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day11.h" #include "Day11.h"
Result Day11::Task1() { Result Day11::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day11::Task2() { Result Day11::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day11 : public Day { class Day11 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day12.h" #include "Day12.h"
Result Day12::Task1() { Result Day12::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day12::Task2() { Result Day12::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day12 : public Day { class Day12 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day13.h" #include "Day13.h"
Result Day13::Task1() { Result Day13::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day13::Task2() { Result Day13::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day13 : public Day { class Day13 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day14.h" #include "Day14.h"
Result Day14::Task1() { Result Day14::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day14::Task2() { Result Day14::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day14 : public Day { class Day14 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day15.h" #include "Day15.h"
Result Day15::Task1() { Result Day15::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day15::Task2() { Result Day15::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day15 : public Day { class Day15 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day16.h" #include "Day16.h"
Result Day16::Task1() { Result Day16::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day16::Task2() { Result Day16::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day16 : public Day { class Day16 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day17.h" #include "Day17.h"
Result Day17::Task1() { Result Day17::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day17::Task2() { Result Day17::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day17 : public Day { class Day17 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day18.h" #include "Day18.h"
Result Day18::Task1() { Result Day18::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day18::Task2() { Result Day18::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day18 : public Day { class Day18 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day19.h" #include "Day19.h"
Result Day19::Task1() { Result Day19::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day19::Task2() { Result Day19::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day19 : public Day { class Day19 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day20.h" #include "Day20.h"
Result Day20::Task1() { Result Day20::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day20::Task2() { Result Day20::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day20 : public Day { class Day20 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day21.h" #include "Day21.h"
Result Day21::Task1() { Result Day21::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day21::Task2() { Result Day21::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day21 : public Day { class Day21 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day22.h" #include "Day22.h"
Result Day22::Task1() { Result Day22::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day22::Task2() { Result Day22::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day22 : public Day { class Day22 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day23.h" #include "Day23.h"
Result Day23::Task1() { Result Day23::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day23::Task2() { Result Day23::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day23 : public Day { class Day23 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day24.h" #include "Day24.h"
Result Day24::Task1() { Result Day24::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day24::Task2() { Result Day24::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day24 : public Day { class Day24 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

@ -1,9 +1,9 @@
#include "Day25.h" #include "Day25.h"
Result Day25::Task1() { Result Day25::Task1() {
return Day::Task1(); return Day::Task1();
} }
Result Day25::Task2() { Result Day25::Task2() {
return Day::Task2(); return Day::Task2();
} }

@ -4,7 +4,7 @@
class Day25 : public Day { class Day25 : public Day {
protected: protected:
Result Task1() override; Result Task1() override;
Result Task2() override; Result Task2() override;
}; };

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