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/main.cpp
2022-12-02 18:42:16 -05:00

107 lines
2 KiB
C++

#include <utility>
#include <iostream>
#include <fstream>
#include <vector>
enum class Play : int {
FUCKYOU = 0,
ROCK = 1,
PAPER = 2,
SCISSORS = 3,
};
enum class Result : char {
WIN = 'W',
LOSS = 'L',
TIE = 'T',
};
typedef std::pair<Play, Play> game_t;
Play char_to_play(char c) {
switch(c) {
case 'A':
case 'X':
return Play::ROCK;
case 'B':
case 'Y':
return Play::PAPER;
case 'C':
case 'Z':
return Play::SCISSORS;
}
return Play::FUCKYOU;
}
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_play(line[2]);
games.push_back(std::make_pair(opponent, mine));
}
return games;
}
Result judge_game(game_t game) {
auto [opponent, me] = game;
if(opponent == me)
return Result::TIE;
if(opponent == Play::ROCK && me == Play::SCISSORS)
return Result::LOSS;
if(opponent == Play::SCISSORS && me == Play::ROCK)
return Result::WIN;
if((int)opponent > (int)me)
return Result::LOSS;
else
return Result::WIN;
}
int score_game(game_t game, Result result) {
int score = 0;
if(result == Result::WIN)
score = 6;
else if(result == Result::TIE)
score = 3;
score += (int)game.second;
return score;
}
int main(int argc, char *argv[]) {
auto games = read_file(argv[1]);
int total = 0;
for(auto game : games) {
auto [opponent, me] = game;
std::cout << (int)opponent << " " << (int)me << std::endl;
auto result = judge_game(game);
std::cout << (char)result << std::endl;
int score = score_game(game, result);
std::cout << score << std::endl;
total += score;
}
std::cout << total << std::endl;
return 0;
}