-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_2.h
More file actions
162 lines (137 loc) · 4.12 KB
/
day_2.h
File metadata and controls
162 lines (137 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// Created by Ryan Avery on 12/6/2022.
//
#ifndef ADVENT_OF_CODE_2022_DAY_2_H
#define ADVENT_OF_CODE_2022_DAY_2_H
#include <string>
#include <vector>
#include <sstream>
#include <tuple>
#include <map>
#include <numeric>
class Round
{
public:
enum Shape {
Rock = 1,
Paper = 2,
Scissors = 3
};
enum Parser {
OldParse = 0,
NewParse = 1
};
Round(Shape opp, Shape yours) : _opp(opp), _yours(yours) {}
Round(const std::string& round, Parser parser)
{
_opp = charToMove(round.c_str()[0]);
if (parser == OldParse) {
_yours = charToMove(round.c_str()[2]);
}
else if (parser == NewParse)
{
auto outcome = charToOutcome(round.c_str()[2]);
_yours = getMoveForOutcome(_opp, outcome);
}
}
[[nodiscard]] int score() const
{
return shapeScore() + outcomeScore();
}
private:
enum Outcome
{
Lose = 0,
Draw = 3,
Win = 6
};
[[nodiscard]] Shape getMoveForOutcome(Shape opp, Outcome outcome) const
{
const auto round = std::find_if(outcomeList.cbegin(), outcomeList.cend(),
[opp, outcome](RoundState state){
return state.opp == opp && state.outcome == outcome;
});
return round->yours;
}
[[nodiscard]] int shapeScore() const
{
return static_cast<int>(_yours);
}
// Order is Opponent, Yours, Outcome
struct RoundState
{
Shape opp;
Shape yours;
Outcome outcome;
};
const std::vector<RoundState> outcomeList {
{Shape::Rock, Shape::Rock, Outcome::Draw},
{Shape::Rock, Shape::Paper, Outcome::Win},
{Shape::Rock, Shape::Scissors, Outcome::Lose},
{Shape::Paper, Shape::Rock, Outcome::Lose},
{Shape::Paper, Shape::Paper, Outcome::Draw},
{Shape::Paper, Shape::Scissors, Outcome::Win},
{Shape::Scissors, Shape::Rock, Outcome::Win},
{Shape::Scissors, Shape::Paper, Outcome::Lose},
{Shape::Scissors, Shape::Scissors, Outcome::Draw},
};
[[nodiscard]] int outcomeScore() const
{
const auto round = std::find_if(outcomeList.cbegin(), outcomeList.cend(),
[opp=_opp, yours=_yours](RoundState state){
return state.opp == opp && state.yours == yours;
});
return static_cast<int>(round->outcome);
}
static Shape charToMove(char in) {
const std::map<char, Shape> kMoveMap =
{
{'A', Shape::Rock},
{'B', Shape::Paper},
{'C', Shape::Scissors},
{'X', Shape::Rock},
{'Y', Shape::Paper},
{'Z', Shape::Scissors},
};
return kMoveMap.at(in);
}
static Outcome charToOutcome(char in) {
const std::map<char, Outcome> kOutcomeMap =
{
{'X', Outcome::Lose},
{'Y', Outcome::Draw},
{'Z', Outcome::Win},
};
return kOutcomeMap.at(in);
}
Shape _opp;
Shape _yours;
};
class StrategyGuide {
public:
using RoundList = std::vector<Round>;
StrategyGuide(const std::string& in, Round::Parser parser)
{
_roundList = parseStringData(in, parser);
}
[[nodiscard]] int getTotalScore() const
{
auto totalScore = std::accumulate(_roundList.begin(), _roundList.end(), 0, [](int acc, const Round& round){
return acc + round.score();
});
return totalScore;
}
private:
static RoundList parseStringData(const std::string& inData, Round::Parser parser)
{
RoundList result;
auto ss = std::stringstream{inData};
for (std::string line; std::getline(ss, line, '\n');) {
Round thisRound(line, parser);
result.push_back(thisRound);
}
return result;
}
RoundList _roundList;
};
#endif //ADVENT_OF_CODE_2022_DAY_2_H