-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2-2.java
57 lines (49 loc) · 1.82 KB
/
day2-2.java
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
class day2_2 {
public static void main(String[] aaa) throws Exception {
BufferedReader in = new BufferedReader(new FileReader(new File("inputs/day2")));
String line;
int safe = 0;
while ((line = in.readLine()) != null) {
int[] report = Arrays.stream(line.split(" ")).mapToInt(Integer::parseInt).toArray();
int i = 0;
boolean increasing = report[report.length - 1] - report[0] > 0;
while (i < report.length - 1) {
if (
report[i + 1] - report[i] > 0 != increasing ||
Math.abs(report[i + 1] - report[i]) > 3 ||
Math.abs(report[i + 1] - report[i]) < 1
) {
if (
i != 0 &&
(
report[i + 1] - report[i - 1] > 0 != increasing ||
Math.abs(report[i + 1] - report[i - 1]) > 3 ||
Math.abs(report[i + 1] - report[i - 1]) < 1
)
) {
break;
}
if (
i < report.length - 2 &&
(
report[i + 2] - report[i] > 0 != increasing ||
Math.abs(report[i + 2] - report[i]) > 3 ||
Math.abs(report[i + 2] - report[i]) < 1
)
) {
break;
}
}
i++;
}
if (i == report.length - 1) {
safe++;
}
}
System.out.println(safe);
}
}