@@ -30,30 +30,42 @@ boolean isSafe(List<Integer> report, boolean dampener) {
30
30
final var failedIndices = getFailedIndices (report , direction );
31
31
if (dampener && !failedIndices .isEmpty ()) {
32
32
for (var i = 0 ; i < report .size (); i ++) {
33
- var clone = new ArrayList <>(report );
34
- clone .remove (i );
33
+ final var clone = getReducedClone (report , i );
35
34
if (isSafe (clone , false )) return true ;
36
35
}
37
36
}
38
37
return failedIndices .isEmpty ();
39
38
}
40
39
40
+ private static ArrayList <Integer > getReducedClone (List <Integer > report , int i ) {
41
+ var clone = new ArrayList <>(report );
42
+ clone .remove (i );
43
+ return clone ;
44
+ }
45
+
41
46
private static Set <Integer > getFailedIndices (List <Integer > report , Direction direction ) {
42
47
Set <Integer > failedIndices = new HashSet <>();
43
- for (var i = 1 ; i < report .size (); i ++) {
44
- var diff = getDiff (report , i );
45
- if (isNotSafeDistance (diff )) {
46
- failedIndices .add (i - 1 );
47
- }
48
-
49
- Direction localDir = getDirection (diff );
50
- if (localDir != direction ) {
51
- failedIndices .add (i - 1 );
52
- }
48
+ for (var index = 1 ; index < report .size (); index ++) {
49
+ var diff = getDiff (report , index );
50
+ addToFailedIndicesIfUnsafeDistance (diff , failedIndices , index );
51
+ addToFailedIndicesIfDirectionChanges (direction , diff , failedIndices , index );
53
52
}
54
53
return failedIndices ;
55
54
}
56
55
56
+ private static void addToFailedIndicesIfDirectionChanges (Direction direction , int diff , Set <Integer > failedIndices , int i ) {
57
+ Direction localDir = getDirection (diff );
58
+ if (localDir != direction ) {
59
+ failedIndices .add (i - 1 );
60
+ }
61
+ }
62
+
63
+ private static void addToFailedIndicesIfUnsafeDistance (int diff , Set <Integer > failedIndices , int i ) {
64
+ if (isNotSafeDistance (diff )) {
65
+ failedIndices .add (i - 1 );
66
+ }
67
+ }
68
+
57
69
private static int getDiff (List <Integer > report , int i ) {
58
70
return report .get (i ) - report .get (i - 1 );
59
71
}
0 commit comments