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/part2 .txt" ;
16
+ final static String FILE = "com/zayzou/day00/input .txt" ;
17
17
18
18
static {
19
19
digitsMap .put ("one" , "1" );
@@ -37,6 +37,7 @@ public static void main(String[] args) {
37
37
String last = "" ;
38
38
while ((line = br .readLine ()) != null ) {
39
39
final String l = line ;
40
+ last ="" ;
40
41
for (int i = 0 ; i < line .length (); i ++) {
41
42
final int index = i ;
42
43
if (Character .isDigit (line .charAt (i ))) {
@@ -53,25 +54,30 @@ public static void main(String[] args) {
53
54
}
54
55
}
55
56
for (int i = line .length () - 1 ; i >= 0 ; i --) {
56
- final int index = i ;
57
57
if (Character .isDigit (line .charAt (i ))) {
58
58
last = String .valueOf (line .charAt (i ));
59
59
break ;
60
60
}
61
- }
62
61
63
- if (last .isBlank ()) {
64
- int maxIndex = -1 ;
65
62
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
+ }
71
70
}
71
+
72
+ }
73
+ if (!last .isBlank ()) {
74
+ break ;
72
75
}
76
+
73
77
}
74
- System .out .println (line + " -> " + first + "" + last );
78
+
79
+
80
+ System .out .println ("calibration values : for " +line + " are " + first + "" + last );
75
81
sum += Integer .valueOf (first + "" + last );
76
82
}
77
83
System .out .println (sum );
0 commit comments