Compare commits

...

2 Commits

  1. 47
      src/days/10/Day10.cpp
  2. 18
      src/main.cpp

@ -1,9 +1,52 @@
#include "Day10.h" #include "Day10.h"
Result Day10::Task1() { Result Day10::Task1() {
return Day::Task1(); int sum = 0;
int R = 1;
int cycle = 1;
for (const string &line : input){
int count = 1;
int add = 0;
if (line.starts_with("addx")){
add = stoi(line.substr(5));
count = 2;
}
for (int i = 0; i < count; i++){
if ((cycle - 20) % 40 == 0){
sum += cycle * R;
}
cycle++;
}
R += add;
}
return to_string(sum);
} }
Result Day10::Task2() { Result Day10::Task2() {
return Day::Task2(); string image = "\n";
int R = 1;
int cycle = 0;
for (const string &line : input){
int count = 1;
int add = 0;
if (line.starts_with("addx")){
add = stoi(line.substr(5));
count = 2;
}
for (int i = 0; i < count; i++) {
char pixel = '.';
int y = cycle / 40;
int x = cycle - (y * 40);
if (x == R || x == R - 1 || x == R + 1)
pixel = '#';
image += pixel;
cycle++;
if (cycle % 40 == 0)
image += "\n";
}
R += add;
}
return image;
} }

@ -56,6 +56,17 @@ string getSessionKey() {
return key; return key;
} }
void parseArgument(const string& arg, int &dayNum, bool &useTestInput, int &testFetchIndex){
size_t tIndex = arg.find('T');
if (tIndex == string::npos) {
dayNum = stoi(arg);
return;
}
dayNum = stoi(arg.substr(0, tIndex));
useTestInput = true;
testFetchIndex = stoi(arg.substr(tIndex + 1));
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
auto key = getSessionKey(); auto key = getSessionKey();
@ -64,9 +75,10 @@ int main(int argc, char *argv[]) {
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
string arg = argv[i]; string arg = argv[i];
bool useTestInput = arg.size() > 1 && arg[1] == 'T'; bool useTestInput = false;
int testFetchIndex = useTestInput && arg.size() > 2 ? arg[2] - '0' : 0; int testFetchIndex;
int dayNum = std::stoi(arg); int dayNum;
parseArgument(arg, dayNum, useTestInput, testFetchIndex);
cout << "Running day " << dayNum << endl; cout << "Running day " << dayNum << endl;
Input input = getInput(dayNum, key, useTestInput, testFetchIndex); Input input = getInput(dayNum, key, useTestInput, testFetchIndex);
Day *day = days[dayNum - 1]; Day *day = days[dayNum - 1];

Loading…
Cancel
Save