This repository has been archived on 2023-12-03. You can view files and clone it, but cannot push or open issues or pull requests.
thomas_2022/day_2/main2.cpp
2022-12-02 18:42:16 -05:00

116 lines
2.3 KiB
C++

#include <utility>
#include <iostream>
#include <fstream>
#include <vector>
enum class Play : int {
ROCK = 1,
PAPER = 2,
SCISSORS = 3,
};
enum class Result : char {
WIN = 'W',
LOSS = 'L',
TIE = 'T',
};
typedef std::pair<Play, Result> game_t;
Play char_to_play(char c) {
switch(c) {
case 'A':
return Play::ROCK;
case 'B':
return Play::PAPER;
case 'C':
return Play::SCISSORS;
}
throw;
}
Result char_to_result(char c) {
switch(c) {
case 'X':
return Result::LOSS;
case 'Y':
return Result::TIE;
case 'Z':
return Result::WIN;
}
throw;
}
Play cheat(Play opponent, Result result) {
if(result == Result::WIN) {
if(opponent == Play::ROCK) return Play::PAPER;
if(opponent == Play::PAPER) return Play::SCISSORS;
if(opponent == Play::SCISSORS) return Play::ROCK;
} else if(result == Result::LOSS) {
if(opponent == Play::ROCK) return Play::SCISSORS;
if(opponent == Play::PAPER) return Play::ROCK;
if(opponent == Play::SCISSORS) return Play::PAPER;
} else {
return opponent;
}
throw;
}
std::vector<game_t> read_file(std::string path) {
std::fstream file;
file.open(path);
std::string line;
std::vector<game_t> games;
while(file) {
std::getline(file, line);
if(line == "")
break;
auto opponent = char_to_play(line[0]);
auto mine = char_to_result(line[2]);
games.push_back(std::make_pair(opponent, mine));
}
return games;
}
int score_game(Play play, Result result) {
int score = 0;
if(result == Result::WIN)
score = 6;
else if(result == Result::TIE)
score = 3;
score += (int)play;
return score;
}
int main(int argc, char *argv[]) {
auto games = read_file(argv[1]);
int total = 0;
for(auto game : games) {
auto [opponent, result] = game;
auto me = cheat(opponent, result);
std::cout << (int)opponent << " " << (int)me << std::endl;
int score = score_game(me, result);
std::cout << score << std::endl;
total += score;
}
std::cout << total << std::endl;
return 0;
}