From 4c675f464fd21803e85fd090d4c024218f39634f Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Tue, 13 Dec 2022 16:39:13 +0100 Subject: [PATCH] Simplify Inspired by https://github.com/AjaxGb/advent-2022/blob/master/src/day13/main.rs --- src/day13.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) 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()) } } } -- 2.45.2