From: Greg Burri Date: Tue, 13 Dec 2022 15:39:13 +0000 (+0100) Subject: Simplify X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=4c675f464fd21803e85fd090d4c024218f39634f;p=advent_of_code_2022.git Simplify Inspired by https://github.com/AjaxGb/advent-2022/blob/master/src/day13/main.rs --- diff --git a/src/day13.rs b/src/day13.rs index cb0b4d9..ceced61 100644 --- a/src/day13.rs +++ b/src/day13.rs @@ -38,25 +38,22 @@ impl Signal { chars.push(' '); parse_chars(&mut chars.windows(2)) } + + fn as_slice(&self) -> &[Self] { + if let Signal::List(l) = self { + l.as_slice() + } else { + std::slice::from_ref(self) + } + } } impl Ord for Signal { fn cmp(&self, other: &Signal) -> Ordering { - match (self, other) { - (Signal::Value(v1), Signal::Value(v2)) => v1.cmp(v2), - (v1 @ Signal::Value(_), l2 @ Signal::List(_)) => Signal::List(vec![v1.clone()]).cmp(l2), - (l1 @ Signal::List(_), v2 @ Signal::Value(_)) => { - l1.cmp(&Signal::List(vec![v2.clone()])) - } - (Signal::List(l1), Signal::List(l2)) => { - for i in 0..l1.len().min(l2.len()) { - match l1[i].cmp(&l2[i]) { - Ordering::Equal => (), - other => return other, - } - } - l1.len().cmp(&l2.len()) - } + if let (Signal::Value(v1), Signal::Value(v2)) = (self, other) { + v1.cmp(v2) + } else { + self.as_slice().cmp(other.as_slice()) } } }