-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.cpp
More file actions
127 lines (106 loc) · 3.22 KB
/
Copy pathday2.cpp
File metadata and controls
127 lines (106 loc) · 3.22 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
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <vector>
// hisHand: A rock B paper C scissors
// myHand: X rock Y paper Z scissors
// 1 pt 2 pt 3pt
// Win 6pt Draw 3pt Lose 0pt
// Example input:
/*
A Y
B X
C Z
*/
const std::string inputFilePath = "inputs/day2.txt";
const int WIN_POINTS = 6;
const int DRAW_POINTS = 3;
const int LOSE_POINTS = 0;
std::map<char,int> myHandScores = {{'X',1}, {'Y',2}, {'Z',3}};
std::map<char,char> hisValuesTranslation = {{'A','X'}, {'B','Y'}, {'C','Z'}};
int calculateEx1(){
int totalScore = 0;
std::ifstream ifs(inputFilePath, std::ifstream::in);
std::string match;
while(std::getline(ifs, match)){
const char hisHand = match.at(0);
const char myHand = match.at(2);
// Calculate my hand's score
const int value = myHandScores[myHand];
totalScore += value;
// Calculate outcome and it's score
const char hisHandTranslated = hisValuesTranslation[hisHand];
if(hisHandTranslated == myHand){
totalScore += DRAW_POINTS;
}
else switch (hisHandTranslated)
{
case 'X':
if (myHand == 'Y'){
totalScore += WIN_POINTS;
}
break;
case 'Y':
if (myHand == 'Z'){
totalScore += WIN_POINTS;
}
break;
case 'Z':
if (myHand == 'X'){
totalScore += WIN_POINTS;
}
break;
};
}
ifs.close();
return totalScore;
}
int calculateEx2(){
// X need to lose, Y need to draw, Z need to win,
std::map<char,int> outcomeScores = {{'X', LOSE_POINTS}, {'Y', DRAW_POINTS}, {'Z', WIN_POINTS}};
int totalScore = 0;
std::ifstream ifs(inputFilePath, std::ifstream::in);
std::string match;
while(std::getline(ifs, match)){
const char hisHand = match.at(0);
const char outcome = match.at(2);
// Get score from outcome
totalScore += outcomeScores.at(outcome);
// Get score from my hand
const char hisHandTranslated = hisValuesTranslation.at(hisHand);
switch (outcome)
{
case 'Y': // draw
totalScore += myHandScores.at(hisHandTranslated);
break;
case 'Z': // win
switch (hisHandTranslated)
{
case 'Z':
totalScore += myHandScores.at('X');
break;
default: // X, Y
totalScore += myHandScores.at((char)(hisHandTranslated + 1));
}
break;
case 'X': // lose
switch (hisHandTranslated)
{
case 'X':
totalScore += myHandScores.at('Z');
break;
default: // Y, Z
totalScore += myHandScores.at((char)(hisHandTranslated - 1));
}
break;
}
}
ifs.close();
return totalScore;
}
int main(){
std::cout << "--Ex1 Output: " << calculateEx1() << std::endl;
std::cout << "--Ex2 Output: " << calculateEx2() << std::endl;
return 0;
}