80 lines
1.7 KiB
C++
80 lines
1.7 KiB
C++
|
#include <fstream>
|
||
|
#include <iostream>
|
||
|
#include <set>
|
||
|
#include <string>
|
||
|
#include <vector>
|
||
|
|
||
|
std::vector<std::string> SplitStr(std::string str, const std::string &delim)
|
||
|
{
|
||
|
std::vector<std::string> rtn;
|
||
|
|
||
|
for(auto split = str.find(delim); split != std::string::npos; split = str.find(delim))
|
||
|
{
|
||
|
rtn.push_back(str.substr(0, split));
|
||
|
str = str.substr(split + delim.size()); // chop off beginning
|
||
|
}
|
||
|
if(str.size())
|
||
|
rtn.push_back(str);
|
||
|
|
||
|
return rtn;
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
|
||
|
for(std::string line; std::getline(ifs, line); )
|
||
|
{
|
||
|
if(line == "")
|
||
|
continue;
|
||
|
|
||
|
auto sections = SplitStr(line, ",");
|
||
|
if(sections.size() != 2)
|
||
|
{
|
||
|
std::cerr << "Incorrect amount of data for line: " << line << std::endl;
|
||
|
exit(-1);
|
||
|
}
|
||
|
|
||
|
auto elf_a = SplitStr(sections[0], "-");
|
||
|
auto elf_b = SplitStr(sections[1], "-");
|
||
|
|
||
|
if(elf_a.size() != 2 || elf_b.size() != 2)
|
||
|
{
|
||
|
std::cerr << "Incorrect amount of data for line: " << line << std::endl;
|
||
|
exit(-2);
|
||
|
}
|
||
|
|
||
|
int elf_a_min = std::atoi(elf_a[0].c_str());
|
||
|
int elf_a_max = std::atoi(elf_a[1].c_str());
|
||
|
int elf_b_min = std::atoi(elf_b[0].c_str());
|
||
|
int elf_b_max = std::atoi(elf_b[1].c_str());
|
||
|
|
||
|
if((elf_a_min <= elf_b_min && elf_a_max >= elf_b_max) ||
|
||
|
(elf_b_min <= elf_a_min && elf_b_max >= elf_a_max))
|
||
|
++total;
|
||
|
|
||
|
// sort
|
||
|
if(elf_a_min > elf_b_min)
|
||
|
{
|
||
|
std::swap(elf_a_min, elf_b_min);
|
||
|
std::swap(elf_a_max, elf_b_max);
|
||
|
}
|
||
|
|
||
|
if(elf_b_min <= elf_a_max)
|
||
|
++total_pt2;
|
||
|
}
|
||
|
|
||
|
std::cout << " Total: " << total << std::endl;
|
||
|
std::cout << "PT2 Total: " << total_pt2 << std::endl;
|
||
|
|
||
|
return 0;
|
||
|
}
|