Advent-of-Code/2022/1/main.cpp
2022-12-01 14:11:53 -05:00

58 lines
954 B
C++

#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
int main()
{
std::ifstream ifs("data.txt");
if(!ifs.is_open())
{
std::cerr << "Couldn't open file." << std::endl;
return -1;
}
static bool last_line_blank = false;
std::vector<long> numbers;
long total = 0;
long most = 0;
for(std::string line; std::getline(ifs, line); )
{
if(line == "")
{
if(!last_line_blank)
{
numbers.push_back(total);
if(total > most)
most = total;
total = 0;
}
last_line_blank = true;
}
else
{
last_line_blank = false;
total += std::atoi(line.c_str());
}
}
std::cout << "Most: " << most << std::endl;
std::sort(numbers.begin(), numbers.end());
if(numbers.size())
{
int three = 3;
long total_total = 0;
for(int i = numbers.size() - 1; i >= 0; i--)
{
if(three-- > 0)
total_total += numbers[i];
}
std::cout << "Top 3: " << total_total << std::endl;
}
return 0;
}