Completed day2
This commit is contained in:
parent
43a925864a
commit
022b27d847
2500
input/day02
2500
input/day02
File diff suppressed because it is too large
Load Diff
@ -1,11 +1,108 @@
|
||||
#include "../aoc2022.h"
|
||||
|
||||
#define WIN 6
|
||||
#define DRAW 3
|
||||
#define LOSS 0
|
||||
|
||||
enum EGameMoves {
|
||||
eRock = 1,
|
||||
ePaper = 2,
|
||||
eScissors = 3
|
||||
};
|
||||
|
||||
struct SGameMove {
|
||||
EGameMoves move, losesTo, winsTo;
|
||||
};
|
||||
|
||||
enum ERules {
|
||||
eTheirRules,
|
||||
eMyRules
|
||||
};
|
||||
|
||||
template <typename Output>
|
||||
struct SDay02 : public SDay<Output> {
|
||||
//Class constructor
|
||||
using SDay<Output>::SDay;
|
||||
|
||||
//Map to store the collection of alises and plays
|
||||
std::map<char, SGameMove*> pairs;
|
||||
std::map<EGameMoves, EGameMoves> losingMoves;
|
||||
|
||||
SGameMove rock{ eRock, ePaper, eScissors };
|
||||
SGameMove paper{ ePaper, eScissors, eRock };
|
||||
SGameMove scissors{ eScissors, eRock, ePaper };
|
||||
|
||||
//Solutions to both days
|
||||
Output part1() override final { return Output(); }
|
||||
Output part2() override final { return Output(); }
|
||||
Output part1() override final {
|
||||
//'A' 'X' = rock and loses to paper
|
||||
pairs['A'] = &rock;
|
||||
pairs['X'] = &rock; // Means Lose
|
||||
//'B' 'Y' = paper and loses to scissors
|
||||
pairs['B'] = &paper;
|
||||
pairs['Y'] = &paper; //Draw
|
||||
//'C' 'Z' = scissors and loses to rock
|
||||
pairs['C'] = &scissors;
|
||||
pairs['Z'] = &scissors; //Win
|
||||
|
||||
return playGame(eTheirRules);
|
||||
}
|
||||
Output part2() override final {
|
||||
return playGame(eMyRules);
|
||||
}
|
||||
|
||||
int theirRules(const SGameMove *player1, const SGameMove *player2) {
|
||||
|
||||
if (player1->losesTo == player2->move)
|
||||
{//We Win
|
||||
return (WIN + player2->move);
|
||||
}
|
||||
|
||||
if (player1->move == player2->move)
|
||||
{//We Draw
|
||||
return (DRAW + player2->move);
|
||||
}
|
||||
|
||||
//We Lose
|
||||
return (LOSS + player2->move);
|
||||
}
|
||||
|
||||
int myRules(const SGameMove *player1, const SGameMove *player2) {
|
||||
if (player2->move == EGameMoves::eRock)
|
||||
{//We must Lose
|
||||
return (LOSS + player1->winsTo);
|
||||
}
|
||||
if (player2->move == EGameMoves::ePaper)
|
||||
{//We must Draw
|
||||
return (DRAW + player1->move);
|
||||
}
|
||||
//We must Win
|
||||
return (WIN + player1->losesTo);
|
||||
}
|
||||
|
||||
int playGame(ERules rules) {
|
||||
this->input.clear();
|
||||
this->input.seekg(0, std::ios::beg);
|
||||
std::string line{ "" };
|
||||
int score{ 0 };
|
||||
|
||||
SGameMove *player1, *player2;
|
||||
|
||||
while (std::getline(this->input, line))
|
||||
{
|
||||
player1 = pairs[line[0]];
|
||||
player2 = pairs[line[2]];
|
||||
|
||||
if (rules == eTheirRules)
|
||||
{//part1
|
||||
score += theirRules(player1, player2);
|
||||
}
|
||||
else
|
||||
{//part2
|
||||
score += myRules(player1, player2);
|
||||
}
|
||||
}
|
||||
return score;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user