2024 day 2

This commit is contained in:
eriedaberrie 2024-12-01 21:17:50 -08:00
parent cb95e2dc3f
commit 40b7af94ca
2 changed files with 76 additions and 0 deletions

29
2024/2/main-1.cpp Normal file
View file

@ -0,0 +1,29 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int total = 0;
string line;
while (getline(cin, line) && !line.empty()) {
istringstream l(line);
vector<int> t;
while (!l.eof()) {
int x;
l >> x;
t.push_back(x);
}
int pos = (t[1] > t[0]) ? 1 : -1;
for (int i = 0; i < t.size() - 1; i++) {
int diff = (t[i + 1] - t[i]) * pos;
if (diff < 1 || diff > 3) {
goto next;
}
}
total++;
next:;
}
cout << total << '\n';
}

47
2024/2/main-2.cpp Normal file
View file

@ -0,0 +1,47 @@
#include <bits/stdc++.h>
using namespace std;
bool check(const auto &t) {
int pos = (t[1] > t[0]) ? 1 : -1;
for (int i = 0; i < t.size() - 1; i++) {
int diff = (t[i + 1] - t[i]) * pos;
if (diff < 1 || diff > 3) {
return false;
}
}
return true;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int total = 0;
string line;
while (getline(cin, line) && !line.empty()) {
istringstream l(line);
vector<int> t;
while (!l.eof()) {
int x;
l >> x;
t.push_back(x);
}
if (check(t)) {
total++;
} else {
for (int i = 0; i < t.size(); i++) {
vector<int> tt;
for (int j = 0; j < t.size(); j++) {
if (j == i)
continue;
tt.push_back(t[j]);
}
if (check(tt)) {
total++;
break;
}
}
}
}
cout << total << '\n';
}