Skip to content

Commit c0f6564

Browse files
committed
feat: day03a
1 parent e8b1efb commit c0f6564

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

day03a/src/main.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn number_in_array(path_file: String) -> u32 {
2828
for (i, line) in str.enumerate() {
2929
let width = line.len() + 1;
3030
for (j, c) in line.chars().enumerate() {
31-
if !c.is_alphanumeric() && !(c == '.') {
31+
if c.is_ascii_punctuation() && !(c == '.') {
3232
let neighbours_index = _neighbours(width, i, j);
3333
for neighbour in neighbours_index {
3434
if text.chars().nth(neighbour).unwrap_or(' ').is_alphanumeric() {
@@ -45,10 +45,34 @@ fn number_in_array(path_file: String) -> u32 {
4545
.map(|(i, _)| i)
4646
.collect();
4747

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+
4869
println!("Text:\n{}", text);
4970
println!("Next Symbol:\n{}", next_symbol);
5071

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));
5276
return res;
5377
}
5478

0 commit comments

Comments
 (0)