-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.java
49 lines (43 loc) · 1.45 KB
/
day02.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
import java.io.*;
import java.util.*;
import static java.lang.System.in;
import static java.lang.System.out;
public class day02 {
public static void main(String[] args) {
var safes = new int[]{0, 0};
new BufferedReader(new InputStreamReader(in)).lines().forEach(line -> {
var report = new Report(line);
if (report.safe()) {
safes[0]++;
safes[1]++;
} else if (report.canBeSafe()) {
safes[1]++;
}
});
out.println(safes[0] + " " + safes[1]);
}
static class Report extends ArrayList<Integer> {
Report(String input) {
super(Arrays.stream(input.split("\\s")).map(Integer::valueOf).toList());
}
Report(List<Integer> source, int except) {
super(source);
remove(except);
}
boolean safe() {
boolean increasing = getFirst().compareTo(getLast()) < 0;
for (int i = 1; i < size(); i++) {
if (increasing && (get(i) - get(i - 1) <= 0 || get(i) - get(i - 1) > 3) ||
!increasing && (get(i) - get(i - 1) >= 0 || get(i - 1) - get(i) > 3)) {
return false;
}
}
return true;
}
boolean canBeSafe() {
for (int i = 0; i < size(); i++)
if (new Report(this, i).safe()) return true;
return false;
}
}
}