Added some missing old files and new day2 of 2024.
This commit is contained in:
2
2024/2/Makefile
Normal file
2
2024/2/Makefile
Normal file
@@ -0,0 +1,2 @@
|
||||
a.out: main_pt2.cpp
|
||||
clang++ -std=c++2b -g -O0 main_pt2.cpp
|
1000
2024/2/data.txt
Normal file
1000
2024/2/data.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
2024/2/data_test.txt
Normal file
6
2024/2/data_test.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
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;
|
||||
}
|
55
2024/2/main_pt2.cpp
Normal file
55
2024/2/main_pt2.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#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;
|
||||
}
|
Reference in New Issue
Block a user