-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc21_1.cpp
55 lines (50 loc) · 928 Bytes
/
aoc21_1.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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Day 1.1: count how many times the depth increases
void solve1a(string fname) {
ifstream in(fname);
if (in.fail()) {
cout << "file open failed\n";
return;
}
int prev;
in >> prev;
int curr;
int res = 0;
while (in >> curr) {
if (curr > prev) {
res++;
}
prev = curr;
}
cout << res << endl;
}
// Day 1.2: count how many times the sum of a sliding window of 3 increases
void solve1b(string fname) {
ifstream in(fname);
if (in.fail()) {
cout << "file open failed\n";
return;
}
int window[4] = {0};
for (int i = 0; i < 3; i++) {
in >> window[i];
}
int res = 0;
while (in >> window[3]) {
if (window[3] > window[0]) {
res++;
}
for (int i = 0; i < 3; i++) {
window[i] = window[i + 1];
}
}
cout << res << endl;
}
int main() {
solve1a("aoc2021_1a_in.txt");
solve1b("aoc2021_1a_in.txt");
return 0;
}