-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.cpp
73 lines (62 loc) · 1.98 KB
/
day2.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
#include "utils/helpers.hpp"
bool sameSign(int a, int b) {
return (a < 0 && b < 0 || a > 0 && b > 0);
}
bool diffInRange(int a) {
return (abs(a) <= 3 && a != 0);
}
std::vector<int> removeElt(const std::vector<int>& src, std::vector<int>::const_iterator toRemove) {
std::vector<int> cpyRemoved;
for (std::vector<int>::const_iterator cpyIt = src.begin(); cpyIt != src.end(); ++cpyIt) {
if (cpyIt != toRemove) {
cpyRemoved.push_back(*cpyIt);
}
}
return cpyRemoved;
}
bool filter(const std::vector<int>& nbrs, bool tolerate) {
int mem;
bool started = false;
int memDif = 0;
int dif;
for (std::vector<int>::const_iterator cit = nbrs.begin(); cit != nbrs.end(); ++cit) {
if (!started) {
started = true;
mem = *cit;
}
else {
dif = mem - *cit;
if (!diffInRange(dif) || (memDif != 0 && !sameSign(dif, memDif)))
{
if (tolerate) {
if (cit - 1 != nbrs.begin())
return (filter(removeElt(nbrs, cit), false) | filter(removeElt(nbrs, cit - 1), false) | filter(removeElt(nbrs, cit - 2), false));
return (filter(removeElt(nbrs, cit), false) | filter(removeElt(nbrs, cit - 1), false));
}
return false;
}
memDif = dif;
mem = *cit;
}
}
return true;
}
int main() {
ui part;
std::ifstream input;
std::vector<int> nbrs;
inputLib::extracted extracted;
int result = 0;
if (getFileAndPart(2, input, part))
return errno;
while (extracted.second != EOF) {
extracted = inputLib::extractNextNumber(input);
nbrs.push_back(extracted.first.value());
if (extracted.second == '\n' || extracted.second == EOF) {
result += filter(nbrs, part != 1);
nbrs.clear();
}
}
std::cout << "result is " << result << '\n';
return 0;
}