-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.cpp
63 lines (56 loc) · 1.54 KB
/
01.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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <deque>
class SumIncrease {
public:
SumIncrease(int elemCount) : elemCount_(elemCount) {}
bool operator()(int x);
int count() const { return count_; }
private:
int elemCount_;
int prev_{ -1 };
int count_{ 0 };
bool first_{ true };
std::deque<int> values_;
};
bool SumIncrease::operator()(int x) {
values_.push_back(x);
if (values_.size() < elemCount_) return false;
if (!first_) {
values_.pop_front();
int sum = std::accumulate(values_.cbegin(), values_.cend(), 0);
if (sum > prev_) {
prev_ = sum;
++count_;
return true;
}
prev_ = sum;
}
else {
prev_ = std::accumulate(values_.cbegin(), values_.cend(), 0);
first_ = false;
}
return false;
}
void partOne(const std::vector<int>& depths) {
auto functor = std::for_each(depths.cbegin(), depths.cend(), SumIncrease(1));
std::cout << "Part One: " << functor.count() << std::endl;
}
void partTwo(const std::vector<int>& depths) {
auto functor = std::for_each(depths.cbegin(), depths.cend(), SumIncrease(3));
std::cout << "Part Two: " << functor.count() << std::endl;
}
int main(int argc, char* argv[]) {
std::ifstream dataFile(argv[1]);
std::string line;
std::vector<int> depths;
while (std::getline(dataFile, line)) {
depths.push_back(std::stoi(line));
}
dataFile.close();
partOne(depths);
partTwo(depths);
}