-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDec2.cpp
More file actions
154 lines (111 loc) · 2.44 KB
/
Copy pathDec2.cpp
File metadata and controls
154 lines (111 loc) · 2.44 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
/*
PART ONE
A = ROCK
B = PAPER
C = SCISSORS
X = ROCK 1
Y = PAPER 2
Z = SCISSORS 3
WIN 6
DRAW 3
LOSS 0
IF ROUND IS U vs V (OPPONENT vs PLAYER)
CONVERT ACCORDING TO ROCK=0,PAPER=1,SCISSOR=2
THEN
WIN IF (V-U)mod 3 = 1 OR -2
DRAW IF (V-U)mod 3 = 0
LOSS IF (V-U)mod 3 = 2 OR -1
*/
/*
PART TWO
A = ROCK 1
B = PAPER 2
C = SCISSORS 3
X = LOSS 2
Y = DRAW 0
Z = WIN 1
WIN 6
DRAW 3
LOSS 0
IF OPPONENT PLAYS U AND EXPECTED OUTCOME IS V
CONVERT V ACCORDING TO WIN=1, DRAW=0, LOSS=2
PLAYER NEEDS TO PLAY (U+V)mod 3
*/
int convert(char player){
switch (player){
case 'A':
case 'X':
return 0;
case 'B':
case 'Y':
return 1;
case 'C':
case 'Z':
return 2;
default:
std::cerr<<"error\n";
return -1;
}
}
int convert2(char player){
switch (player){
case 'Y':
return 0;
case 'A':
case 'Z':
return 1;
case 'B':
case 'X':
return 2;
case 'C':
return 3;
default:
std::cerr<<"error\n";
return -1;
}
}
int playRound(char opponent,char player){
int opValue{convert(opponent)};
int plValue{convert(player)};
int score{};
int roundValue{(plValue-opValue)%3};
if (roundValue==1 || roundValue==-2) score += 6;
else if (roundValue==0) score += 3;
score += (plValue +1);
return score;
}
int playRound2(char opponent,char expected){
int opValue{convert2(opponent)};
int exValue{convert2(expected)};
int plValue{(opValue+exValue)%3};
if (plValue==0) plValue=3;
int score{};
if (exValue==1) score += 6;
else if (exValue==0) score += 3;
score += plValue;
return score;
}
int main(){
std::ifstream file("data2.txt");
std::string str{};
char opponent{};
char player{};
int score_1{};
int score_2{};
while(getline(file,str)){
std::stringstream ss{str};
ss>>opponent>>player;
//PART ONE
score_1 += playRound(opponent,player);
//PART TWO
score_2 += playRound2(opponent,player);
}
std::cout<<score_1<<'\n';
std::cout<<score_2<<'\n';
return 0;
}