13
13
public class Day01 {
14
14
15
15
final static HashMap <String , String > digitsMap = new HashMap <>();
16
- final static String FILE = "com/zayzou/day00/input .txt" ;
16
+ final static String FILE = "com/zayzou/day00/part2 .txt" ;
17
17
18
- public static void main ( String [] args ) {
18
+ static {
19
19
digitsMap .put ("one" , "1" );
20
20
digitsMap .put ("two" , "2" );
21
21
digitsMap .put ("three" , "3" );
@@ -25,6 +25,9 @@ public static void main(String[] args) {
25
25
digitsMap .put ("seven" , "7" );
26
26
digitsMap .put ("eight" , "8" );
27
27
digitsMap .put ("nine" , "9" );
28
+ }
29
+
30
+ public static void main (String [] args ) {
28
31
29
32
//use auto close ressource to read the file
30
33
try (BufferedReader br = new BufferedReader (new FileReader (FILE ))) {
@@ -33,7 +36,6 @@ public static void main(String[] args) {
33
36
String first = "" ;
34
37
String last = "" ;
35
38
while ((line = br .readLine ()) != null ) {
36
-
37
39
final String l = line ;
38
40
for (int i = 0 ; i < line .length (); i ++) {
39
41
final int index = i ;
@@ -42,89 +44,39 @@ public static void main(String[] args) {
42
44
break ;
43
45
}
44
46
Optional <String > firstOptional = digitsMap .keySet ().stream ()
47
+ .filter (key -> index + key .length () < l .length ())//to prevent out of bound
45
48
.filter (key -> key .equals (l .substring (index , index + key .length ())))
46
49
.findFirst ();
47
50
if (firstOptional .isPresent ()) {
48
51
first = digitsMap .get (firstOptional .get ());
49
52
break ;
50
53
}
51
-
52
54
}
53
-
54
55
for (int i = line .length () - 1 ; i >= 0 ; i --) {
55
56
final int index = i ;
56
57
if (Character .isDigit (line .charAt (i ))) {
57
58
last = String .valueOf (line .charAt (i ));
58
59
break ;
59
60
}
60
- Optional <String > lastOptional = digitsMap .keySet ().stream ()
61
- .filter (ee -> l .lastIndexOf (ee ) == ((l .length ()) - ee .length ()))
62
- .findFirst ();
63
- if (lastOptional .isPresent ()) {
64
- last = digitsMap .get (lastOptional .get ());
65
- break ;
66
- }
67
61
}
68
62
69
- sum +=Integer .valueOf (first + "" + last );
70
-
63
+ if (last .isBlank ()) {
64
+ int maxIndex = -1 ;
65
+ for (Map .Entry <String , String > entry : digitsMap .entrySet ()) {
66
+ var number = entry .getKey ();
67
+ int index = line .lastIndexOf (number );
68
+ if (maxIndex < index ) {
69
+ last = entry .getValue ();
70
+ maxIndex = index ;
71
+ }
72
+ }
73
+ }
74
+ System .out .println (line + " -> " + first + "" + last );
75
+ sum += Integer .valueOf (first + "" + last );
71
76
}
72
77
System .out .println (sum );
73
78
} catch (IOException ex ) {
74
79
throw new RuntimeException (ex .getMessage ());
75
80
}
76
-
77
-
78
81
}
79
-
80
- private static int sumOfAllValues (String line ) {
81
-
82
- //look for the first digit
83
- OptionalInt first = line .chars ()
84
- .filter (Character ::isDigit )
85
- .map (Character ::getNumericValue )
86
- .findFirst ();
87
-
88
- //look for the last digit
89
- Optional <Integer > last = IntStream .range (0 , line .length ())
90
- .boxed ()
91
- .sorted (Collections .reverseOrder ())
92
- .filter (i -> Character .isDigit (line .charAt (i )))
93
- .map (i -> Character .getNumericValue (line .charAt (i )))
94
- .findFirst ();
95
-
96
- //create the number from the 2 digits
97
- Integer number = Integer .valueOf (first .getAsInt () + "" + last .get ());
98
- return number ;
99
- }
100
-
101
-
102
- public static void part2 (String line ) {
103
- //look for the last digit
104
- // Map.Entry<String, String> entry =
105
- // getFirstDigit(line);
106
- //look for the last digit
107
- /* Map.Entry<String, Integer> last = digitsMap.entrySet().stream()
108
- .filter(ee -> line.lastIndexOf(ee.getKey()) == ((line.length()) - ee.getKey().length()))
109
- .findFirst().get();*/
110
- // System.out.println(entry.getValue() + "" + last.getValue());
111
-
112
- }
113
-
114
- /*
115
- private static Integer getFirstDigit(String line) {
116
- return digitsMap.entrySet().stream()
117
-
118
- .filter(ee -> line.indexOf(ee.getKey()) == 0)
119
- .findFirst().get().getValue();
120
- }
121
- */
122
-
123
- /* private static Integer getLastDigit(String line) {
124
- return digitsMap.entrySet().stream()
125
- .filter(ee -> line.lastIndexOf(ee.getKey()) == ((line.length()) - ee.getKey().length()))
126
- .findFirst().get().getValue();
127
- }*/
128
-
129
-
130
82
}
0 commit comments