-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.cpp
119 lines (98 loc) · 2.87 KB
/
day02.cpp
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
/**
* @file day02.cpp
* @author Miguel Ángel Moreno Castro
* @date Dec 2, 2022
*/
#include <utility>
#include <vector>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
vector<pair<char, char>> Read(const string & file){
ifstream fi(file);
pair<char, char> temp;
vector<pair<char, char>> strategy;
string aux;
if (fi.is_open()){
while (getline(fi, aux)){
temp.first = aux.at(0);
temp.second = aux.at(2);
strategy.push_back(temp);
}
}
fi.close();
return strategy;
}
int ScorePerRound(const pair<char, char> & input){
int score;
switch (input.first) {
case 'A':
if (input.second == 'Y')
score = 2 + 6;
else
score = input.second == 'X' ? 1 + 3 : 3;
break;
case 'B':
if (input.second == 'Z')
score = 3 + 6;
else
score = input.second == 'Y' ? 2 + 3 : 1;
break;
case 'C':
if (input.second == 'X')
score = 1 + 6;
else
score = input.second == 'Z' ? 3 + 3 : 2;
break;
}
return score;
}
vector<pair<char, char>> Guidance (const vector<pair<char, char>> & strategy){
vector<pair<char, char>> aux;
pair<char, char> temp;
for (int i = 0; i < strategy.size(); i++){
temp.first = strategy.at(i).first;
switch (strategy.at(i).second) {
case 'X':
if (temp.first == 'A')
temp.second = 'Z';
else
temp.second = temp.first == 'B' ? 'X' : 'Y';
break;
case 'Y':
if (temp.first == 'A')
temp.second = 'X';
else
temp.second = temp.first == 'B' ? 'Y' : 'Z';
break;
case 'Z':
if (temp.first == 'A')
temp.second = 'Y';
else
temp.second = temp.first == 'B' ? 'Z' : 'X';
break;
}
aux.push_back(temp);
}
return aux;
}
int Puzzle1(const vector<pair<char, char>> & strategy){
int total = 0;
for (int i = 0; i < strategy.size(); i++)
total += ScorePerRound(strategy.at(i));
return total;
}
int Puzzle2(const vector<pair<char, char>> & strat){
int total = 0;
vector<pair<char, char>> newstrategy = Guidance(strat);
for (int i = 0; i < newstrategy.size(); i++)
total += ScorePerRound(newstrategy.at(i));
return total;
}
int main(int argc, char * argv[]){
vector<pair<char, char>> strategy = Read(argv[1]);
cout << "\nThe total score for the first puzzle would be " << Puzzle1(strategy);
cout << "\nThe total score for the second puzzle would be " << Puzzle2(strategy);
return 0;
}