added day 2 task 2

This commit is contained in:
matthias@arch 2022-12-03 03:32:31 +01:00
parent 9fc8f8d864
commit 26e1fe2d5e
2 changed files with 2517 additions and 3 deletions

View File

@ -1,11 +1,15 @@
#include <cstdio> #include <cstdio>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <stdexcept>
#include <string> #include <string>
constexpr char ROCK = 1; constexpr char ROCK = 1;
constexpr char PAPER = 2; constexpr char PAPER = 2;
constexpr char SCISSORS = 3; constexpr char SCISSORS = 3;
constexpr char LOSS = 1;
constexpr char DRAW = 2;
constexpr char WIN = 3;
inline int get_result(const char& myChoice, const char& opponentChoice) { inline int get_result(const char& myChoice, const char& opponentChoice) {
if (myChoice == SCISSORS and opponentChoice == ROCK) { return 0; } if (myChoice == SCISSORS and opponentChoice == ROCK) { return 0; }
@ -15,21 +19,31 @@ inline int get_result(const char& myChoice, const char& opponentChoice) {
return 0; return 0;
} }
inline char get_choice_for_result(const char& result, const int& opponentChoice) {
if (result == DRAW) { return opponentChoice; }
else if (result == WIN) { return opponentChoice < 3 ? opponentChoice + 1 : 1; }
else if (result == LOSS) { return opponentChoice > 1 ? opponentChoice - 1 : 3; }
throw std::runtime_error("Reached end of get_choice_for_result");
}
int main() { int main() {
int score = 0; int score_1 = 0;
int score_2 = 0;
auto is = std::ifstream("scores.txt"); auto is = std::ifstream("scores.txt");
std::string line; std::string line;
if (is.is_open()) { if (is.is_open()) {
for (std::string line; std::getline(is, line); ) { for (std::string line; std::getline(is, line); ) {
char other = line.at(0) + 1 - 'A'; // convert to 1,2,3; char other = line.at(0) + 1 - 'A'; // convert to 1,2,3;
char mine = line.at(2) + 1 - 'X'; // convert to 1,2,3 char mine = line.at(2) + 1 - 'X'; // convert to 1,2,3
score += mine + get_result(mine, other); score_1 += mine + get_result(mine, other);
score_2 += (mine - 1) * 3 + get_choice_for_result(mine, other);
} }
} }
else { else {
std::cerr << "Could not open file scores.txt" << std::endl; std::cerr << "Could not open file scores.txt" << std::endl;
return 1; return 1;
} }
std::cout << "My score is " << score << std::endl; std::cout << "If XYZ is Rock/Paper/Scissors, my score is " << score_1 << std::endl;
std::cout << "If XYZ is Loss/Draw/Win, my score is " << score_2 << std::endl;
return 0; return 0;
} }

2500
2/scores.txt Normal file

File diff suppressed because it is too large Load Diff