53 lines
1.2 KiB
C++
53 lines
1.2 KiB
C++
|
#include <iostream>
|
||
|
#include <fstream>
|
||
|
#include <string>
|
||
|
#include <vector>
|
||
|
#include <algorithm>
|
||
|
|
||
|
|
||
|
int main(int argc, char *argv[]) {
|
||
|
if(argc != 2) {
|
||
|
std::cerr << "Usage " << argv[0] << " <input>" << std::endl;
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
std::ifstream file;
|
||
|
file.open(argv[1]);
|
||
|
|
||
|
std::string line;
|
||
|
std::vector<int> groups;
|
||
|
int sum = 0;
|
||
|
while(file) {
|
||
|
std::getline(file, line);
|
||
|
std::cout << "got " << line << std::endl;
|
||
|
|
||
|
if(line == "") {
|
||
|
if(sum > 0)
|
||
|
groups.push_back(sum);
|
||
|
sum = 0;
|
||
|
} else {
|
||
|
sum += std::stoi(line);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for(int sum : groups) {
|
||
|
std::cout << "sum " << sum << std::endl;
|
||
|
}
|
||
|
std::cout << "Parsed " << groups.size() << " groups" << std::endl;
|
||
|
|
||
|
int maxs[3];
|
||
|
auto max = std::max_element(groups.begin(), groups.end());
|
||
|
maxs[0] = *max;
|
||
|
groups.erase(max);
|
||
|
max = std::max_element(groups.begin(), groups.end());
|
||
|
maxs[1] = *max;
|
||
|
groups.erase(max);
|
||
|
max = std::max_element(groups.begin(), groups.end());
|
||
|
maxs[2] = *max;
|
||
|
groups.erase(max);
|
||
|
|
||
|
std::cout << "Max is " << maxs[0] + maxs[1] + maxs[2] << std::endl;
|
||
|
|
||
|
return 0;
|
||
|
}
|