Advent-of-Code/2022/1/main.cpp

58 lines
954 B
C++
Raw Normal View History

2022-12-01 09:14:56 -05:00
#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;
}