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

View file

@ -24,29 +24,30 @@ 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 {
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) {
return false; return false;
} }
if ordering == None { if !Self::safe_level_pair(*a, *b, ordering) {
ordering = Some(a.cmp(&b)) 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;
} }
} }
@ -54,25 +55,13 @@ impl Report {
} }
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;
} }
} }