I wrote 2 versions of problem 22 on Project Euler:
Using a 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?
Any pointers (see what I did there) or tips on improving my code would be great.
V1: Simply computes and outputs the summed value:
#include <iostream>
using std::endl;
using std::cout;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <algorithm>
using std::vector;
int main(void){
vector<string> names;
ifstream input("p022_names.txt");
string tempName;
while (getline(input, tempName, ',')){
tempName.erase(tempName.begin());
tempName.pop_back();
names.push_back(tempName);
}
sort(names.begin(), names.end());
names.shrink_to_fit(); //works with my compiler
long ultimateTotal = 0;
int i = 0;
for (auto name : names){
int sum = 0;
for (int j = 0; j != name.size(); ++j){
sum += (name[j] - 64);
}
ultimateTotal+=(++i*sum);
}
cout << ultimateTotal << endl;
return 0;
}
V2: Outputs name, name alphabetical value, and name score to a file:
#include <iostream>
using std::endl;
using std::cin;
using std::cout;
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <algorithm>
void formatInput(vector<string>&, ifstream&);
void alphaNumSum(vector<string>, vector<int>&);
void totalScore(vector<int>, vector<int>&);
void outputFinal(vector<string>, vector<int>, vector<int>, ofstream&);
int main(void){
vector<string> names;
ifstream input("p022_names.txt");
formatInput(names, input);
sort(names.begin(), names.end());
names.shrink_to_fit();
vector<int> alphSum;
alphaNumSum(names, alphSum);
vector<int> tScore;
totalScore(alphSum, tScore);
ofstream output("nameScores.txt");
outputFinal(names, alphSum, tScore, output);
return 0;
}
void formatInput(vector<string>& names, ifstream& input){
string tempName;
while (getline(input, tempName, ',')){
tempName.erase(tempName.begin());
tempName.pop_back();
names.push_back(tempName);
}
}
void alphaNumSum(vector<string> names, vector<int>& alphSum){
for (auto name : names){
int sum = 0;
for (int j = 0; j != name.size(); ++j){
sum += (name[j] - 64);
}
alphSum.push_back(sum);
}
}
void totalScore(vector<int> alphSum, vector<int>& tScore){
int score;
int i = 1;
for (vector<int>::iterator l_begin = alphSum.begin(); l_begin != alphSum.end(); ++l_begin, ++i){
score = i * *l_begin;
tScore.push_back(score);
}
}
void outputFinal(vector<string> names, vector<int> alphSum, vector<int> tScore, ofstream& output){
vector<string>::iterator begin = names.begin();
vector<string>::iterator end = names.end() - 1;
vector<int>::iterator l_begin = alphSum.begin();
vector<int>::iterator l_end = alphSum.end();
vector<int>::iterator score_begin = tScore.begin();
vector<int>::iterator score_end = tScore.end();
long long ultimateTotal = 0;
for (/* n/a */; begin != end, l_begin != l_end, score_begin != score_end; ++begin, ++l_begin, ++score_begin){
output << "NAME: " << *begin << "\tALPHNUMERIC SUM: " << *l_begin << "\tTOTAL SCORE: " << *score_begin << endl;
ultimateTotal += *score_begin;
}
cout << "TOTAL NAME SCORES: " << ultimateTotal << endl;
}