master
Benjamin Kraft 2 years ago
parent c1df946d1e
commit dcee17005f
  1. 41
      src/days/02/Day02.cpp
  2. 3
      src/days/02/Day02.h

@ -1,9 +1,46 @@
#include "Day02.h"
int Day02::getScore(char opponent, char me) {
int outcome;
if (opponent == me)
outcome = 3;
else if (opponent == me + 1 || opponent == me - 2)
outcome = 0;
else
outcome = 6;
return outcome + me - 64;
}
Result Day02::Task1() {
return Day::Task1();
int score = 0;
for (string line : input){
char predict = line.at(0);
char answer = char(line.at(2) - 23);
score += getScore(predict, answer);
}
return to_string(score);
}
Result Day02::Task2() {
return Day::Task2();
int score = 0;
for (string line : input){
char predict = line.at(0);
char outcome = line.at(2);
char answer;
if (outcome == 'X'){
if (predict > 'A') answer = char(predict - 1);
else answer = 'C';
}
if (outcome == 'Y'){
answer = predict;
}
if (outcome == 'Z'){
if (predict < 'C') answer = char(predict + 1);
else answer = 'A';
}
score += getScore(predict, answer);
}
return to_string(score);
}

@ -7,4 +7,7 @@ protected:
Result Task1() override;
Result Task2() override;
private:
// chars A, B, C
static int getScore(char, char);
};
Loading…
Cancel
Save