aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
This commit is contained in:
parent
96775c800b
commit
deafcf3662
151
day2/day.cpp
151
day2/day.cpp
@ -1,95 +1,90 @@
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <errno.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <errno.h>
|
||||
#include <vector>
|
||||
|
||||
std::vector<std::string> splitString(const std::string& str) {
|
||||
std::istringstream iss(str);
|
||||
std::vector<std::string> words;
|
||||
std::string word;
|
||||
std::vector<std::string> splitString(const std::string &str) {
|
||||
std::istringstream iss(str);
|
||||
std::vector<std::string> words;
|
||||
std::string word;
|
||||
|
||||
while (iss >> word) {
|
||||
words.push_back(word);
|
||||
}
|
||||
while (iss >> word) {
|
||||
words.push_back(word);
|
||||
}
|
||||
|
||||
return words;
|
||||
return words;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ifstream inputfile("example");
|
||||
std::string input;
|
||||
|
||||
int res = 0;
|
||||
if (inputfile.is_open()) {
|
||||
std::ifstream inputfile("input");
|
||||
std::string input;
|
||||
|
||||
// Read each line from the file and store it in the
|
||||
// 'line' variable.
|
||||
std::string line;
|
||||
std::vector<std::string> num_s;
|
||||
std::vector<int> num;
|
||||
|
||||
while (std::getline(inputfile, line)) {
|
||||
num_s = splitString(line);
|
||||
num.clear();
|
||||
std::transform(num_s.begin(), num_s.end(), std::back_inserter(num), [](const std::string& s) {
|
||||
return std::stoi(s);
|
||||
});
|
||||
bool safe = true;
|
||||
int old_n = -1;
|
||||
for (int n : num) {
|
||||
if(old_n == -1) {
|
||||
old_n = n;
|
||||
continue;
|
||||
}
|
||||
if ((old_n - n) > 2 || (old_n - n) > 0) {
|
||||
safe = false;
|
||||
}
|
||||
old_n = n;
|
||||
}
|
||||
if (safe) {
|
||||
res++;
|
||||
continue;
|
||||
}
|
||||
safe = true;
|
||||
old_n = -1;
|
||||
for (int n : num) {
|
||||
if(old_n == -1) {
|
||||
old_n = n;
|
||||
continue;
|
||||
}
|
||||
if ((old_n - n) <= -3 && (old_n - n) < 0) {
|
||||
safe = false;
|
||||
}
|
||||
old_n = n;
|
||||
}
|
||||
if (safe) {
|
||||
res++;
|
||||
}
|
||||
}
|
||||
int res = 0;
|
||||
if (inputfile.is_open()) {
|
||||
|
||||
|
||||
// Read each line from the file and store it in the
|
||||
// 'line' variable.
|
||||
std::string line;
|
||||
std::vector<std::string> num_s;
|
||||
std::vector<int> num;
|
||||
|
||||
// Close the file stream once all lines have been
|
||||
// read.
|
||||
inputfile.close();
|
||||
}
|
||||
else {
|
||||
// Print an error message to the standard error
|
||||
// stream if the file cannot be opened.
|
||||
std::cerr << "Unable to open file!" << std::endl;
|
||||
return ENOENT;
|
||||
while (std::getline(inputfile, line)) {
|
||||
num_s = splitString(line);
|
||||
num.clear();
|
||||
std::transform(num_s.begin(), num_s.end(), std::back_inserter(num),
|
||||
[](const std::string &s) { return std::stoi(s); });
|
||||
bool safe = true;
|
||||
int old_n = -1;
|
||||
for (int n : num) {
|
||||
if (old_n == -1) {
|
||||
old_n = n;
|
||||
continue;
|
||||
}
|
||||
if ((old_n - n) > 2 || (old_n - n) < 0 || old_n == n) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
old_n = n;
|
||||
}
|
||||
if (safe) {
|
||||
res++;
|
||||
std::cerr << line << "\n";
|
||||
continue;
|
||||
}
|
||||
safe = true;
|
||||
old_n = -1;
|
||||
for (int n : num) {
|
||||
if (old_n == -1) {
|
||||
old_n = n;
|
||||
continue;
|
||||
}
|
||||
if ((n - old_n) > 3 || (n - old_n) < 0 || old_n == n) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
old_n = n;
|
||||
}
|
||||
if (safe) {
|
||||
std::cerr << line << "\n";
|
||||
res++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::cout << "\n\n" << res << "\n\n";
|
||||
|
||||
return 0;
|
||||
// Close the file stream once all lines have been
|
||||
// read.
|
||||
inputfile.close();
|
||||
} else {
|
||||
// Print an error message to the standard error
|
||||
// stream if the file cannot be opened.
|
||||
std::cerr << "Unable to open file!" << std::endl;
|
||||
return ENOENT;
|
||||
}
|
||||
|
||||
std::cout << "\n\n" << res << "\n\n";
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user