Added some missing old files and new day2 of 2024.
This commit is contained in:
83
2024/2/main.cpp
Normal file
83
2024/2/main.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#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;
|
||||
|
||||
for(std::string line; std::getline(ifs, line); )
|
||||
{
|
||||
if(line == "")
|
||||
continue;
|
||||
|
||||
std::istringstream input(line);
|
||||
long *last = nullptr;
|
||||
bool *increasing = nullptr;
|
||||
bool safe = true;
|
||||
for(std::string value; std::getline(input, value, ' '); )
|
||||
{
|
||||
long current;
|
||||
// Get the value:
|
||||
try {
|
||||
current = std::atoi(value.c_str());
|
||||
} catch (const std::exception &e) {
|
||||
break;
|
||||
}
|
||||
|
||||
// See if it's the first value:
|
||||
if(!last)
|
||||
{
|
||||
last = new long(current);
|
||||
continue;
|
||||
}
|
||||
|
||||
// See if it's the second value:
|
||||
if(!increasing)
|
||||
{
|
||||
increasing = new bool(current > *last);
|
||||
}
|
||||
|
||||
auto diff = current - *last;
|
||||
if(*increasing &&
|
||||
(diff < 1 || diff > 3))
|
||||
{
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
if(!*increasing &&
|
||||
(diff < -3 || diff > -1))
|
||||
{
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if(last)
|
||||
*last = current;
|
||||
}
|
||||
if(increasing && last // actually made it through at least 2 values
|
||||
&& safe)
|
||||
total++;
|
||||
|
||||
if(increasing)
|
||||
delete increasing, increasing = nullptr;
|
||||
if(last)
|
||||
delete last, last = nullptr;
|
||||
}
|
||||
|
||||
std::cout << " Total: " << total << std::endl;
|
||||
std::cout << "PT2 Total: " << total_pt2 << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user