Alternative to Project Euler 22 solution.
Slightly more brittle than the original. This code depends on the input stream being exactly as specified. But on the other side of the coin it's slightly easier to read because you don't have to wonder about std::locale
and code that uses facets
like std::ctype<>
(which few people understand how to use).
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.
What is the total of all the name scores in the file?
#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
#include <set>
#include <numeric>
long scoreName(std::string const& name)
{
return std::accumulate(std::begin(name), std::end(name), 0L,
[](long v1, char x){return v1 + x - 'A' + 1;});
}
struct FirstName
{
// Read a name from a stream.
// Each name is quoted and separated with comma.
// There are no extra spaces on the stream.
// Sample: "DEBBIE","APRIL","LESLIE","CLARA","LUCILLE","JAMIE"
std::string name;
// Read the next name from the input.
// Note does not remove quotes.
friend std::istream& operator>>(std::istream& s, FirstName& data)
{
return std::getline(s, data.name, ',');
}
// When we convert this object to a std::string
// we remove the quotes around the name. In the code
// below this happens as the iterator is de-reference into the set.
operator std::string() const
{
return name.substr(1, name.size() - 2);
}
};
Thus leaving a very simple main.
int main()
{
std::ifstream data("euler/e22.data");
std::set<std::string> names{std::istream_iterator<FirstName>(data),
std::istream_iterator<FirstName>()};
long score = 0;
long loop = 1;
for(auto name: names) {
score += (loop * scoreName(name));
++loop;
}
std::cout << score << "\n";
}