simplified

This commit is contained in:
Acvaxoort 2023-12-03 11:43:35 +01:00
parent 30cd7f0a65
commit 533c270587

View File

@ -57,13 +57,12 @@ fn main() {
}
let mut sum1 = 0;
let mut sum2 = 0;
let mut neighbour_numbers: Vec<i32> = Vec::new();
for row in 0 .. row_counter {
for col in 0 ..col_counter {
let value = adjacency_table[(row * col_counter + col) as usize];
if value == SYMBOL || value == GEAR {
let mut num1 = -1;
let mut num2 = -1;
let mut valid_gear = value == GEAR;
neighbour_numbers.clear();
for offset_row in -1..2 {
for offset_col in -1..2 {
let new_row = row + offset_row;
@ -73,22 +72,16 @@ fn main() {
if new_value >= 0 {
numbers_occur[new_value as usize] = true;
if value == GEAR {
if num1 == -1 {
num1 = new_value;
} else if num1 != new_value {
if num2 == -1 {
num2 = new_value;
} else if num2 != new_value {
valid_gear = false;
if !neighbour_numbers.contains(&new_value) {
neighbour_numbers.push(new_value);
}
}
}
}
}
}
}
if valid_gear && num1 >= 0 && num2 >= 0 {
sum2 += numbers[num1 as usize] * numbers[num2 as usize];
if neighbour_numbers.len() == 2 {
sum2 += numbers[neighbour_numbers[0] as usize] * numbers[neighbour_numbers[1] as usize];
}
}
}