This commit is contained in:
Daniel Flanagan 2024-12-02 12:19:04 -06:00
parent 6f6b8c1d62
commit e7cc1df936

View file

@ -24,55 +24,44 @@ impl FromStr for Report {
impl Report {
fn is_safe(&self, skip_index: Option<usize>) -> bool {
let mut ordering: Option<Ordering> = None;
let skip_index = skip_index.unwrap_or(self.levels.len() + 1);
let max = if skip_index < self.levels.len() {
self.levels.len() - 2
let mut index_sequence: Box<dyn Iterator<Item = &i64>> = if let Some(i) = skip_index {
Box::new(
self.levels[0..i]
.iter()
.chain(self.levels[i + 1..self.levels.len()].iter())
.into_iter(),
)
} else {
self.levels.len() - 1
Box::new(self.levels.iter())
};
for i in 0..max {
let mut ai = i;
let mut bi = ai + 1;
if ai >= skip_index {
ai += 1
};
if bi >= skip_index {
bi += 1
};
let (a, b) = (self.levels[ai], self.levels[bi]);
if !Self::safe_level_pair(a, b, &ordering) {
let a = index_sequence.next().unwrap();
let b = index_sequence.next().unwrap();
let ordering = a.cmp(b);
if ordering == Ordering::Equal {
return false;
}
if !Self::safe_level_pair(*a, *b, ordering) {
return false;
}
println!("{a} {b} {ordering:?} {skip_index:?}");
while let (a, Some(b)) = (b, index_sequence.next()) {
println!("{a} {b} {ordering:?}");
if !Self::safe_level_pair(*a, *b, ordering) {
return false;
}
if ordering == None {
ordering = Some(a.cmp(&b))
}
}
return true;
}
fn is_safe_with_any_single_level_removed(&self) -> bool {
for i in 0..self.levels.len() {
if self.is_safe(Some(i)) {
return true;
}
}
return false;
(0..self.levels.len())
.into_iter()
.any(|i| self.is_safe(Some(i)))
}
fn safe_level_pair(a: i64, b: i64, kind: &Option<Ordering>) -> bool {
if a == b {
return false;
}
if (a - b).abs() > 3 {
return false;
}
if kind.map(|o| o == a.cmp(&b)) == Some(false) {
return false;
}
return true;
fn safe_level_pair(a: i64, b: i64, kind: Ordering) -> bool {
kind == a.cmp(&b) && (a - b).abs() <= 3
}
}