1313public class Day01 {
1414
1515 final static HashMap <String , String > digitsMap = new HashMap <>();
16- final static String FILE = "com/zayzou/day00/part2 .txt" ;
16+ final static String FILE = "com/zayzou/day00/input .txt" ;
1717
1818 static {
1919 digitsMap .put ("one" , "1" );
@@ -37,6 +37,7 @@ public static void main(String[] args) {
3737 String last = "" ;
3838 while ((line = br .readLine ()) != null ) {
3939 final String l = line ;
40+ last ="" ;
4041 for (int i = 0 ; i < line .length (); i ++) {
4142 final int index = i ;
4243 if (Character .isDigit (line .charAt (i ))) {
@@ -53,25 +54,30 @@ public static void main(String[] args) {
5354 }
5455 }
5556 for (int i = line .length () - 1 ; i >= 0 ; i --) {
56- final int index = i ;
5757 if (Character .isDigit (line .charAt (i ))) {
5858 last = String .valueOf (line .charAt (i ));
5959 break ;
6060 }
61- }
6261
63- if (last .isBlank ()) {
64- int maxIndex = -1 ;
6562 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 ;
63+ // two1nine i=7-
64+ if ((i - entry .getKey ().length () + 1 ) >= 0 ) {
65+ String substring = line .substring (i - entry .getKey ().length () + 1 , i + 1 );
66+ if (substring .compareTo (entry .getKey ()) == 0 ) {
67+ last = entry .getValue ();
68+ break ;
69+ }
7170 }
71+
72+ }
73+ if (!last .isBlank ()) {
74+ break ;
7275 }
76+
7377 }
74- System .out .println (line + " -> " + first + "" + last );
78+
79+
80+ System .out .println ("calibration values : for " +line + " are " + first + "" + last );
7581 sum += Integer .valueOf (first + "" + last );
7682 }
7783 System .out .println (sum );
0 commit comments