-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution-part2.cpp
76 lines (71 loc) · 2.15 KB
/
solution-part2.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
std::vector<int> sortVector(std::vector<int>& vec){
std::vector<int> sortedVec;
int max = 0;
int maxIndex = 0;
for (int i = 0; i < vec.size(); i++){
for (int j = 0; j < vec.size(); j++){
if (vec.at(j) > max){
max = vec.at(j);
maxIndex = j;
}
}
sortedVec.push_back(max);
vec.erase(vec.begin() + maxIndex);
max = 0;
maxIndex = 0;
}
return sortedVec;
}
std::vector<std::string> splitString(const std::string& str)
{
std::vector<std::string> tokens;
std::string::size_type pos = 0;
std::string::size_type prev = 0;
while ((pos = str.find('\n', prev)) != std::string::npos) {
tokens.push_back(str.substr(prev, pos - prev));
prev = pos + 1;
}
tokens.push_back(str.substr(prev));
return tokens;
}
std::string readFile(const std::string& fileName){
std::ifstream puzzleFile;
puzzleFile.open(fileName);
std::string puzzleText;
if ( puzzleFile.is_open() ) {
std::stringstream puzzleStream;
puzzleStream << puzzleFile.rdbuf();
puzzleText = puzzleStream.str();
}
puzzleFile.close();
return puzzleText;
}
int * findGreatestCalories(std::vector<std::string>& lines){
std::vector<int> topCalories;
int currentTotal = 0;
for (int i = 0; i < lines.size(); i++){
if (lines.at(i) == "\n" or lines.at(i) == ""){
topCalories.push_back(currentTotal);
currentTotal = 0;
}
else {
currentTotal += std::stoi(lines.at(i));
}
}
topCalories = sortVector(topCalories);
int greatestCaloriesArray[] = {topCalories.at(0), topCalories.at(1), topCalories.at(2)};
return greatestCaloriesArray;
}
int main () {
std::string puzzleText = readFile("puzzle_input.txt");
std::vector<std::string> puzzleLines = splitString(puzzleText);
int *topCalories = findGreatestCalories(puzzleLines);
int total = topCalories[0] + topCalories[1] + topCalories[2];
std::cout << total << std::endl;
return 0;
}