@@ -28,7 +28,7 @@ fn number_in_array(path_file: String) -> u32 {
28
28
for ( i, line) in str. enumerate ( ) {
29
29
let width = line. len ( ) + 1 ;
30
30
for ( j, c) in line. chars ( ) . enumerate ( ) {
31
- if !c . is_alphanumeric ( ) && !( c == '.' ) {
31
+ if c . is_ascii_punctuation ( ) && !( c == '.' ) {
32
32
let neighbours_index = _neighbours ( width, i, j) ;
33
33
for neighbour in neighbours_index {
34
34
if text. chars ( ) . nth ( neighbour) . unwrap_or ( ' ' ) . is_alphanumeric ( ) {
@@ -45,10 +45,34 @@ fn number_in_array(path_file: String) -> u32 {
45
45
. map ( |( i, _) | i)
46
46
. collect ( ) ;
47
47
48
+ // Find adjacent number
49
+ for next_symbol_index in next_symbol_index_list {
50
+ let mut i = 1 ;
51
+ while next_symbol_index + i < text. len ( ) && text. chars ( ) . nth ( next_symbol_index + i) . unwrap_or ( ' ' ) . is_alphanumeric ( ) {
52
+ i += 1 ;
53
+ }
54
+ if !next_symbol_index + i < text. len ( ) {
55
+ i -= 1 ;
56
+ }
57
+ next_symbol. replace_range ( next_symbol_index..next_symbol_index + i, & text[ next_symbol_index..next_symbol_index + i] ) ;
58
+
59
+ i = 1 ;
60
+ while next_symbol_index >= i && text. chars ( ) . nth ( next_symbol_index - i) . unwrap_or ( ' ' ) . is_alphanumeric ( ) {
61
+ i += 1 ;
62
+ }
63
+ if !next_symbol_index >= i {
64
+ i -= 1 ;
65
+ }
66
+ next_symbol. replace_range ( next_symbol_index - i..next_symbol_index, & text[ next_symbol_index - i..next_symbol_index] ) ;
67
+ }
68
+
48
69
println ! ( "Text:\n {}" , text) ;
49
70
println ! ( "Next Symbol:\n {}" , next_symbol) ;
50
71
51
- let res: u32 = 0 ;
72
+ // Return sum of numbers
73
+ let list_numbers = next_symbol. split ( |c : char | c == '.' || c == '\n' ) ;
74
+ let mut res: u32 = 0 ;
75
+ list_numbers. for_each ( |s : & str | res += s. parse :: < u32 > ( ) . unwrap_or ( 0 ) ) ;
52
76
return res;
53
77
}
54
78
0 commit comments