Advent-of-Code/2024/2/main_pt2.cpp

56 lines
999 B
C++
Raw Normal View History

#include <fstream>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <vector>
int main()
{
std::ifstream ifs("data.txt");
if(!ifs.is_open())
{
std::cerr << "Missing data.txt." << std::endl;
return -1;
}
unsigned long total = 0;
unsigned long total_pt2 = 0;
std::vector<std::vector<long>> data;
for(std::string line; std::getline(ifs, line); )
{
if(line == "")
continue;
// add a row for this line:
data.push_back({});
std::istringstream input(line);
// for each value in the line:
for(std::string value; std::getline(input, value, ' '); )
{
try {
// add it to the row
data.back().push_back(std::atoi(value.c_str()));
} catch (const std::exception &e) {
break;
}
}
}
// DEBUG:
for(auto row : data)
{
for(auto col : row)
std::cout << col << ", ";
std::cout << std::endl;
}
std::cout << " Total: " << total << std::endl;
std::cout << "PT2 Total: " << total_pt2 << std::endl;
return 0;
}