-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_2.cpp
121 lines (97 loc) · 2.35 KB
/
day_2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
void print_vec(vector<int> &v) {
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << "\n";
}
vector<int> to_arr_int(string &s) {
string in;
vector<int> out;
for (int i = 0; i < s.size(); i++) {
if (s[i] != ' ') {
in += s[i];
} else {
if (!in.empty()) {
out.push_back(stoi(in));
in = "";
}
}
}
if (!in.empty())
out.push_back(stoi(in));
return out;
}
bool is_decreasing(vector<int>& v){
bool b = true;
int diff;
for (int i = 1; i < v.size(); i++) {
diff = abs(v[i] - v[i - 1]);
b = b && (v[i] < v[i - 1]) && diff >= 1 && diff <= 3;
}
return b;
};
bool is_increasing(vector<int>& v) {
bool b = true;
int diff;
for (int i = 1; i < v.size(); i++) {
diff = abs(v[i] - v[i - 1]);
b = b && (v[i] > v[i - 1]) && diff >= 1 && diff <= 3;
}
return b;
};
bool is_safe(vector<int> &v) { return is_decreasing(v) || is_increasing(v); }
bool is_safe_with_rm(vector<int> &v) {
if (is_increasing(v) || is_decreasing(v)) return true;
bool is_increasing_ = [&]() -> bool {
vector<bool> rem_idx(v.size(), false);
vector<int> temp;
for (int i = 0; i < v.size(); i++) {
rem_idx[i] = true;
for (int j = 0; j < v.size(); j++){
if (rem_idx[j]) continue;
temp.push_back(v[j]);
}
if (is_increasing(temp)) return true;
rem_idx[i] = false;
temp.clear();
}
return false;
}();
bool is_decreasing_ = [&]() -> bool {
vector<bool> rem_idx(v.size(), false);
vector<int> temp;
for (int i = 0; i < v.size(); i++) {
rem_idx[i] = true;
for (int j = 0; j < v.size(); j++){
if (rem_idx[j]) continue;
temp.push_back(v[j]);
}
if (is_decreasing(temp)) return true;
rem_idx[i] = false;
temp.clear();
}
return false;
}();
return is_increasing_ || is_decreasing_;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
string s;
int safe = 0;
int safe_modified = 0;
while (getline(cin, s)) {
vector<int> v = to_arr_int(s);
safe += is_safe(v);
safe_modified += is_safe_with_rm(v);
}
cout << "safe: " << safe << "\n";
cout << "safe modified: " << safe_modified << "\n";
return 0;
}