- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem022.cpp
More file actions
Latest commit
51 lines (48 loc) · 1.2 KB
/
Copy pathproblem022.cpp
File metadata and controls
51 lines (48 loc) · 1.2 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
#include<algorithm>
#include<cstdlib>
#include<fstream>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
intalphabeticalValue(const std::string& name)
{
int val = 0;
for (int i = 0; i < name.size(); ++i) {
val += name[i] - 'A' + 1;
}
return val;
}
intnameScoreTotal(std::vector<std::string> names)
{
std::sort(names.begin(), names.end());
int total = 0;
for (int i = 0; i < names.size(); ++i) {
int score = alphabeticalValue(names[i]) * (i + 1);
total += score;
}
return total;
}
intmain(int argc, char* argv[])
{
if (argc < 2) {
std::cout << "Usage: " << argv[0] << " <input>\n";
return0;
}
std::ifstream file(argv[1]);
if (!file.is_open()) {
std::cerr << "Cannot open " << argv[1] << '\n';
returnEXIT_FAILURE;
}
std::vector<std::string> names;
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string token;
while (std::getline(iss, token, ',')) {
names.push_back(token.substr(1, token.size() - 2));
}
}
std::cout << "Answer: " << nameScoreTotal(names) << '\n';
return0;
}