Skip to content

Commit 3a61a63

Browse files
committed
2021:01: Solve part 2 [cpp]
1 parent 6f4cec4 commit 3a61a63

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

2021/day01/part2/solution.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
#include <fstream>
3+
4+
using namespace std;
5+
6+
const int windowSize = 3;
7+
int window[windowSize];
8+
9+
void shiftWindow(int newValue) {
10+
window[0] = window[1];
11+
window[1] = window[2];
12+
window[2] = newValue;
13+
}
14+
15+
int windowTotal() {
16+
int ret = 0;
17+
for (int i = 0; i < windowSize; i++) ret += window[i];
18+
return ret;
19+
}
20+
21+
int main() {
22+
ifstream inFile ("../input");
23+
int inputNumbers[2000];
24+
25+
int curInNumber;
26+
int curInLineInx = -1;
27+
while (inFile >> curInNumber) {
28+
curInLineInx += 1;
29+
inputNumbers[curInLineInx] = curInNumber;
30+
}
31+
32+
// Set the first window
33+
for (int i = 0; i <= windowSize; i++) window[i] = inputNumbers[i];
34+
35+
int wentDeeperTimes = 0;
36+
for (int i = 3; i < 2000; i++) {
37+
int prev = windowTotal();
38+
shiftWindow(inputNumbers[i]);
39+
int cur = windowTotal();
40+
if (cur > prev) wentDeeperTimes += 1;
41+
}
42+
43+
cout << wentDeeperTimes << endl;
44+
}
45+
46+

0 commit comments

Comments
 (0)