From d031c0be43aa58b3219410244cd5ebf8290429f9 Mon Sep 17 00:00:00 2001 From: Ben C Date: Fri, 5 Dec 2025 09:13:42 -0500 Subject: [PATCH] Doctests for BetterRange::merge --- utils/src/range.rs | 45 ++++++++++++++++++++--------------------- years/2025/src/day_5.rs | 2 +- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/utils/src/range.rs b/utils/src/range.rs index de3f76f..b18f3e4 100644 --- a/utils/src/range.rs +++ b/utils/src/range.rs @@ -1,6 +1,6 @@ use std::{fmt::Debug, ops::Range}; -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)] /// Represents a range of values. /// /// End is exclusive. @@ -55,6 +55,27 @@ impl BetterRange { } /// Merge this range with another. If the ranges do not intersect, [None] is returned. + /// + /// # Example + /// + /// ``` + /// use utils::prelude::*; + /// + /// let a = BetterRange::new(1, 5); + /// let b = BetterRange::new(6, 11); + /// let c = BetterRange::new(4, 8); + /// let d = BetterRange::new(3, 4); + /// + /// + /// assert!(a.merge(&b).is_none(), "Should not merge"); + /// assert!(d.merge(&b).is_none(), "Should not merge"); + /// assert_eq!(a.merge(&c).unwrap(), BetterRange::new(1, 8), "1-5 & 4-8 = 1-8"); + /// assert_eq!(c.merge(&a).unwrap(), BetterRange::new(1, 8), "4-8 & 1-5 = 1-8"); + /// assert_eq!(a.merge(&d).unwrap(), a, "1-5 & 3-4 = 1-5"); + /// assert_eq!(d.merge(&a).unwrap(), a, "3-4 & 1-5 = 1-5"); + /// assert_eq!(c.merge(&d).unwrap(), BetterRange::new(3, 8), "4-8 & 3-4 = 3-8"); + /// assert_eq!(d.merge(&c).unwrap(), BetterRange::new(3, 8), "3-4 & 4-8 = 3-8"); + /// ``` pub fn merge(&self, other: &Self) -> Option { if self.contains_range(other) { Some(*self) @@ -163,25 +184,3 @@ where Self::new(self.start - rhs, self.end - rhs) } } - -impl std::ops::BitAnd for BetterRange -where - T: std::ops::BitAnd, -{ - type Output = Self; - - fn bitand(self, rhs: Self) -> Self::Output { - Self::new(self.start.max(rhs.start), self.end.min(rhs.end)) - } -} - -impl std::ops::BitOr for BetterRange -where - T: std::ops::BitOr, -{ - type Output = Self; - - fn bitor(self, rhs: Self) -> Self::Output { - Self::new(self.start.min(rhs.start), self.end.max(rhs.end)) - } -} diff --git a/years/2025/src/day_5.rs b/years/2025/src/day_5.rs index 98de929..95be44a 100644 --- a/years/2025/src/day_5.rs +++ b/years/2025/src/day_5.rs @@ -16,7 +16,7 @@ impl Day for Day5 { } fn part_2((mut ranges, _): Self::Input) -> Option { - ranges.sort_by(|a, b| a.start.cmp(&b.start).then(a.end.cmp(&b.end))); + ranges.sort(); let mut new_ranges = Vec::with_capacity(ranges.len()); -- 2.51.2