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