aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

This commit is contained in:
PoliEcho 2024-12-12 17:29:05 +01:00
parent 96775c800b
commit deafcf3662

View File

@ -1,14 +1,11 @@
#include <algorithm> #include <algorithm>
#include <cstddef> #include <cstddef>
#include <iostream> #include <errno.h>
#include <string> #include <fstream>
#include <vector>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <vector>
#include <string> #include <string>
#include <fstream> #include <vector>
#include <errno.h>
std::vector<std::string> splitString(const std::string &str) { std::vector<std::string> splitString(const std::string &str) {
std::istringstream iss(str); std::istringstream iss(str);
@ -23,7 +20,7 @@ std::vector<std::string> splitString(const std::string& str) {
} }
int main() { int main() {
std::ifstream inputfile("example"); std::ifstream inputfile("input");
std::string input; std::string input;
int res = 0; int res = 0;
@ -38,9 +35,8 @@ int main() {
while (std::getline(inputfile, line)) { while (std::getline(inputfile, line)) {
num_s = splitString(line); num_s = splitString(line);
num.clear(); num.clear();
std::transform(num_s.begin(), num_s.end(), std::back_inserter(num), [](const std::string& s) { std::transform(num_s.begin(), num_s.end(), std::back_inserter(num),
return std::stoi(s); [](const std::string &s) { return std::stoi(s); });
});
bool safe = true; bool safe = true;
int old_n = -1; int old_n = -1;
for (int n : num) { for (int n : num) {
@ -48,13 +44,15 @@ int main() {
old_n = n; old_n = n;
continue; continue;
} }
if ((old_n - n) > 2 || (old_n - n) > 0) { if ((old_n - n) > 2 || (old_n - n) < 0 || old_n == n) {
safe = false; safe = false;
break;
} }
old_n = n; old_n = n;
} }
if (safe) { if (safe) {
res++; res++;
std::cerr << line << "\n";
continue; continue;
} }
safe = true; safe = true;
@ -64,31 +62,28 @@ int main() {
old_n = n; old_n = n;
continue; continue;
} }
if ((old_n - n) <= -3 && (old_n - n) < 0) { if ((n - old_n) > 3 || (n - old_n) < 0 || old_n == n) {
safe = false; safe = false;
break;
} }
old_n = n; old_n = n;
} }
if (safe) { if (safe) {
std::cerr << line << "\n";
res++; res++;
} }
} }
// Close the file stream once all lines have been // Close the file stream once all lines have been
// read. // read.
inputfile.close(); inputfile.close();
} } else {
else {
// Print an error message to the standard error // Print an error message to the standard error
// stream if the file cannot be opened. // stream if the file cannot be opened.
std::cerr << "Unable to open file!" << std::endl; std::cerr << "Unable to open file!" << std::endl;
return ENOENT; return ENOENT;
} }
std::cout << "\n\n" << res << "\n\n"; std::cout << "\n\n" << res << "\n\n";
return 0; return 0;