1313public class Day01 {
1414
1515 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" ;
1717
18- public static void main ( String [] args ) {
18+ static {
1919 digitsMap .put ("one" , "1" );
2020 digitsMap .put ("two" , "2" );
2121 digitsMap .put ("three" , "3" );
@@ -25,6 +25,9 @@ public static void main(String[] args) {
2525 digitsMap .put ("seven" , "7" );
2626 digitsMap .put ("eight" , "8" );
2727 digitsMap .put ("nine" , "9" );
28+ }
29+
30+ public static void main (String [] args ) {
2831
2932 //use auto close ressource to read the file
3033 try (BufferedReader br = new BufferedReader (new FileReader (FILE ))) {
@@ -33,7 +36,6 @@ public static void main(String[] args) {
3336 String first = "" ;
3437 String last = "" ;
3538 while ((line = br .readLine ()) != null ) {
36-
3739 final String l = line ;
3840 for (int i = 0 ; i < line .length (); i ++) {
3941 final int index = i ;
@@ -42,89 +44,39 @@ public static void main(String[] args) {
4244 break ;
4345 }
4446 Optional <String > firstOptional = digitsMap .keySet ().stream ()
47+ .filter (key -> index + key .length () < l .length ())//to prevent out of bound
4548 .filter (key -> key .equals (l .substring (index , index + key .length ())))
4649 .findFirst ();
4750 if (firstOptional .isPresent ()) {
4851 first = digitsMap .get (firstOptional .get ());
4952 break ;
5053 }
51-
5254 }
53-
5455 for (int i = line .length () - 1 ; i >= 0 ; i --) {
5556 final int index = i ;
5657 if (Character .isDigit (line .charAt (i ))) {
5758 last = String .valueOf (line .charAt (i ));
5859 break ;
5960 }
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- }
6761 }
6862
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 );
7176 }
7277 System .out .println (sum );
7378 } catch (IOException ex ) {
7479 throw new RuntimeException (ex .getMessage ());
7580 }
76-
77-
7881 }
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-
13082}
0 commit comments