-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem22.cpp
More file actions
42 lines (33 loc) · 893 Bytes
/
problem22.cpp
File metadata and controls
42 lines (33 loc) · 893 Bytes
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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
static_assert(sizeof(unsigned long long) == 8, "Type might be too small");
int main()
{
// First, read the names into memory.
std::ifstream f{"names.txt"};
if (!f) {
std::cerr << "Error while opening the file 'names.txt'.\n";
return EXIT_FAILURE;
}
std::vector<std::string> names;
std::string tmp;
while (std::getline(f, tmp, ',')) {
// remove leading and trailing quotation marks
tmp = tmp.substr(1, tmp.length() - 2);
names.push_back(tmp);
}
// Now, sort the names alphabetically.
std::sort(names.begin(), names.end());
// Compute the sum.
unsigned long long sum = 0;
for (std::size_t i = 0; i < names.size(); ++i) {
unsigned int nameSum = 0;
for (auto c : names[i])
nameSum += c - 'A' + 1;
sum += nameSum * (i + 1);
}
std::cout << sum << std::endl;
}