-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
45 lines (40 loc) · 1.18 KB
/
main.cpp
File metadata and controls
45 lines (40 loc) · 1.18 KB
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
int main() {
fstream text_file;
text_file.open("input.txt");
if (text_file.is_open()) {
string line;
int largestNumber {0};
vector<string> calories;
vector<int> elf;
vector<vector<int>> elfs;
while (getline(text_file, line))
{
calories.push_back(line);
}
for (string str : calories) {
if (str.empty()) {
elfs.push_back(elf);
elf.clear();
} else {
elf.push_back(stoi(str));
}
}
vector<int> sortable;
for (vector<int> i : elfs)
{
int value = reduce(i.begin(), i.end());
sortable.push_back(value);
}
sort(sortable.begin(),sortable.end());
cout << "Elf carrying the most Calories, carries " << sortable[sortable.size()-1] << " calories.\n";
cout << "Three top elves carry " << sortable[sortable.size()-1] + sortable[sortable.size()-2] + sortable[sortable.size()-3] << " calories.";
}
return 0;
}