71 lines
1.6 KiB
C++
71 lines
1.6 KiB
C++
#include "../include/useful_funcs.h"
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <errno.h>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
bool isSafe(const std::vector<int> &num, bool checkIncreasing) {
|
|
for (size_t i = 1; i < num.size(); ++i) {
|
|
int diff = checkIncreasing ? (num[i] - num[i - 1]) : (num[i - 1] - num[i]);
|
|
if (diff < 1 || diff > 3) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool isSafeWithDampener(const std::vector<int> &num) {
|
|
// Check if already safe
|
|
if (isSafe(num, false) || isSafe(num, true)) {
|
|
return true;
|
|
}
|
|
|
|
// Try removing each level and check for safety
|
|
for (size_t i = 0; i < num.size(); ++i) {
|
|
std::vector<int> temp = num;
|
|
temp.erase(temp.begin() + i); // Remove the current level
|
|
if (isSafe(temp, false) || isSafe(temp, true)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
int main() {
|
|
std::ifstream inputfile("input");
|
|
std::string input;
|
|
|
|
int res = 0; // Counter for safe reports
|
|
if (inputfile.is_open()) {
|
|
std::string line;
|
|
std::vector<std::string> num_s;
|
|
std::vector<int> num;
|
|
|
|
while (std::getline(inputfile, line)) {
|
|
num_s = splitString(line);
|
|
num.clear();
|
|
|
|
ConvertStringVectorToIntVector(num_s, num);
|
|
|
|
// Check safety with Problem Dampener support
|
|
if (isSafeWithDampener(num)) {
|
|
res++;
|
|
std::clog << line << std::endl;
|
|
}
|
|
}
|
|
|
|
inputfile.close();
|
|
} else {
|
|
std::cerr << "Unable to open file!" << std::endl;
|
|
return ENOENT;
|
|
}
|
|
|
|
std::cout << res << std::endl;
|
|
return 0;
|
|
}
|