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/main.cpp

42 lines
911 B
C++
Raw Permalink 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;
std::cout << "Max is " << *std::max_element(groups.begin(), groups.end()) << std::endl;
return 0;
}