10
10
11
11
public class Day02 {
12
12
private static final Logger LOGGER = LoggerFactory .getLogger (Day02 .class );
13
- private List <String > input ;
13
+ private List <List < Long >> allLists ;
14
14
15
15
public Day02 () {
16
16
}
17
17
18
18
public void parseInput (List <String > input ) {
19
- this .input = input ;
20
- }
21
-
22
- public long solvePart1 () {
23
19
Pattern p = Pattern .compile ("(\\ d+)" );
24
-
25
- List <List <Long >> allLists = new ArrayList <>(input .size ());
20
+ allLists = new ArrayList <>(input .size ());
26
21
27
22
for (String s : input ) {
28
23
Matcher matcher = p .matcher (s );
@@ -36,16 +31,16 @@ public long solvePart1() {
36
31
37
32
allLists .add (list );
38
33
}
34
+ }
39
35
36
+ public long solvePart1 () {
40
37
return allLists .stream ()
41
38
.filter (this ::isSafe )
42
39
.count ();
43
-
44
40
}
45
41
46
42
private boolean isSafe (List <Long > l ) {
47
-
48
- LOGGER .info ("Checking {}" , l );
43
+ LOGGER .debug ("Checking {}" , l );
49
44
Long prev = null ;
50
45
Boolean ascending = null ;
51
46
for (Long i : l ) {
@@ -54,68 +49,52 @@ private boolean isSafe(List<Long> l) {
54
49
continue ;
55
50
}
56
51
57
- if (prev == i ) {
58
- LOGGER .info (" has 2 same values {}" , i );
52
+ if (prev . compareTo ( i ) == 0 ) {
53
+ LOGGER .debug (" has 2 same values {}" , i );
59
54
return false ;
60
55
}
61
56
62
57
if (ascending == null ) {
63
- ascending = prev < i ;
64
- LOGGER .info (" is {}" , ascending ? "ascending" : "descending" );
58
+ ascending = prev . compareTo ( i ) < 0 ;
59
+ LOGGER .debug (" is {}" , ascending ? "ascending" : "descending" );
65
60
} else {
66
61
67
- if (ascending && prev >= i ) {
68
- LOGGER .info (" started as ascending, but is not!" );
62
+ if (ascending && prev . compareTo ( i ) > 0 ) {
63
+ LOGGER .debug (" started as ascending, but is not!" );
69
64
return false ;
70
65
}
71
- if (!ascending && prev <= i ) {
72
- LOGGER .info (" started as descending, but is not!" );
66
+ if (!ascending && prev . compareTo ( i ) < 0 ) {
67
+ LOGGER .debug (" started as descending, but is not!" );
73
68
return false ;
74
69
}
75
70
}
76
71
77
- if (Math .abs (prev - i ) > 3 && Math .abs (prev - i ) > 0 ) {
78
- LOGGER .info (" has too big gap between {} and {}" , prev , i );
72
+ long absDiff = Math .abs (prev - i );
73
+ if (absDiff > 3 ) {
74
+ LOGGER .debug (" has too big gap between {} and {}" , prev , i );
79
75
return false ;
80
76
}
81
77
82
78
prev = i ;
83
79
}
84
80
85
- LOGGER .info (" is safe" );
81
+ LOGGER .debug (" is safe" );
86
82
return true ;
87
83
}
88
84
89
85
public long solvePart2 () {
90
- Pattern p = Pattern .compile ("(\\ d+)" );
91
-
92
- List <List <Long >> allLists = new ArrayList <>(input .size ());
93
-
94
- for (String s : input ) {
95
- Matcher matcher = p .matcher (s );
96
- List <Long > list = new ArrayList <>();
97
-
98
- while (matcher .find ()) {
99
- Long a = Long .valueOf (matcher .group (1 ));
100
- LOGGER .debug ("Reading: {}" , a );
101
- list .add (a );
102
- }
103
-
104
- allLists .add (list );
105
- }
106
-
107
86
return allLists .stream ()
108
87
.filter (this ::isSafeWithoutOneLevel )
109
88
.count ();
110
89
}
111
90
112
91
private boolean isSafeWithoutOneLevel (List <Long > l ) {
113
- List <List <Long >> possibleCombinations = new ArrayList <>();
92
+ List <List <Long >> orderedSelectionWithOneElement = new ArrayList <>(l . size () );
114
93
for (int i = 0 ; i < l .size (); i ++) {
115
94
List <Long > l1 = new ArrayList <>(l );
116
95
l1 .remove (i );
117
- possibleCombinations .add (l1 );
96
+ orderedSelectionWithOneElement .add (l1 );
118
97
}
119
- return possibleCombinations .stream ().anyMatch (this ::isSafe );
98
+ return orderedSelectionWithOneElement .stream ().anyMatch (this ::isSafe );
120
99
}
121
100
}
0 commit comments