-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwentytwo.cpp
More file actions
50 lines (41 loc) · 1.11 KB
/
Copy pathtwentytwo.cpp
File metadata and controls
50 lines (41 loc) · 1.11 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
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <string>
#include <regex>
using namespace std;
const regex delimiter {","};
const string filename {"names.txt"};
// compute sum of letters -> digits
int wordscore(string word) {
int score = 0;
for (auto &ch: word) {
if (ch != '"') {
score += (ch - 'A' + 1);
}
}
return score;
}
int name_scores() {
int result = 0;
vector<string> names;
ifstream csvFile(filename);
// for each line, split by each delimiter
for (string line{}; getline(csvFile, line); ) {
names = vector<string>(sregex_token_iterator(line.begin(),
line.end(),
delimiter, -1), {});
}
sort(names.begin(), names.end());
// multiply word score by position
for (int i = 1; i <= names.size(); i++) {
result += (i * wordscore(names[i-1]));
}
return result;
}
int main() {
int result = name_scores();
cout << "Name scores: " << result << endl;
return 0;
}