This repository has been archived on 2023-12-03. You can view files and clone it, but cannot push or open issues or pull requests.
thomas_2022/day_1/main2.cpp

53 lines
1.2 KiB
C++
Raw Normal View History

2022-12-02 18:41:28 -05:00
#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;
}