fix diagonals FULLY

This commit is contained in:
2025-02-04 16:14:42 -05:00
parent 7c74d45618
commit 24ad9cfafd
2 changed files with 90 additions and 56 deletions

View File

@@ -15,14 +15,53 @@ where
if x > min + T::one() {
let x_lower = x - T::one();
output.push((min..=x_lower).rev().collect());
} else {
output.push(Vec::new());
}
if x + T::one() < max {
output.push(((x + T::one())..=max).collect());
} else {
output.push(Vec::new());
}
output
}
pub fn diag(
i: usize,
j: usize,
min_i: usize,
min_j: usize,
max_i: usize,
max_j: usize,
) -> Vec<Vec<(usize, usize)>> {
let i_chains = split_from(min_i, max_i, i);
let j_chains = split_from(min_j, max_j, j);
vec![
i_chains[0]
.clone()
.into_iter()
.zip(j_chains[0].clone())
.collect(),
i_chains[1]
.clone()
.into_iter()
.zip(j_chains[1].clone())
.collect(),
i_chains[1]
.clone()
.into_iter()
.zip(j_chains[0].clone())
.collect(),
i_chains[0]
.clone()
.into_iter()
.zip(j_chains[1].clone())
.collect(),
]
}
#[cfg(test)]
mod test {
use super::*;
@@ -31,9 +70,9 @@ mod test {
fn split_test() {
assert_eq!(split_from(0, 6, 2), vec![vec![1, 0], vec![3, 4, 5, 6]]);
assert_eq!(split_from(0, 6, 0), vec![vec![1, 2, 3, 4, 5, 6]]);
assert_eq!(split_from(0, 6, 0), vec![vec![], vec![1, 2, 3, 4, 5, 6]]);
assert_eq!(split_from(0, 6, 6), vec![vec![5, 4, 3, 2, 1, 0]]);
assert_eq!(split_from(0, 6, 6), vec![vec![5, 4, 3, 2, 1, 0], vec![]]);
// test out-of-bounds and also generics
assert_eq!(
@@ -41,4 +80,17 @@ mod test {
Vec::<Vec<i16>>::new()
);
}
#[test]
fn diag_test() {
assert_eq!(
diag(2, 3, 0, 0, 7, 7),
vec![
vec![(1, 2), (0, 1)],
vec![(3, 4), (4, 5), (5, 6), (6, 7)],
vec![(3, 2), (4, 1), (5, 0)],
vec![(1, 4), (0, 5)]
]
);
}
}