-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecond.cpp
More file actions
49 lines (44 loc) · 1.28 KB
/
Copy pathsecond.cpp
File metadata and controls
49 lines (44 loc) · 1.28 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
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
int getPower(std::string input) {
std::string numstring = "";
std::string colourstring = "";
std::unordered_map<std::string, int> maxmap = {{"red",0},{"blue",0},{"green",0}};
int number;
for (int i = 0; i < input.length(); i++) {
if (isdigit(input[i])){
numstring += input[i];
} else if (isalpha(input[i])) {
colourstring += input[i];
}
if (input[i] == ',' || input[i] == ';' || i == input.length()-1) {
maxmap[colourstring] = std::max(maxmap[colourstring], std::stoi(numstring));
numstring = "";
colourstring = "";
}
}
return (maxmap["red"] * maxmap["blue"] * maxmap["green"]);
}
int main() {
std::ifstream file("prompt2.txt");
std::string line;
std::string game;
std::string idstring;
int id;
int startindex;
int total = 0;
while (std::getline(file, line)) {
for (int i = 0; i < line.length(); i++) {
if (line[i] == ':') {
startindex = i+1;
break;
}
}
game = line.substr(startindex);
total += getPower(game);
}
std::cout << total << std::endl;
return 0;
}