-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution-part1.cpp
55 lines (51 loc) · 1.48 KB
/
solution-part1.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
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){
int MaxCalories = 0;
int currentTotal = 0;
for (int i = 0; i < lines.size(); i++){
if (lines.at(i) == "\n" or lines.at(i) == ""){
if (currentTotal > MaxCalories){
MaxCalories = currentTotal;
}
currentTotal = 0;
}
else {
currentTotal += std::stoi(lines.at(i));
}
}
return MaxCalories;
}
int main () {
std::string puzzleText = readFile("puzzle_input.txt");
std::vector<std::string> puzzleLines = splitString(puzzleText);
std::cout << findGreatestCalories(puzzleLines) << std::endl;
return 0;
}