Advent of Code solutions

Doctests for BetterRange::merge

bwc9876.dev d031c0be 796138a9

verified
+23 -24
+22 -23
utils/src/range.rs
··· 1 1 use std::{fmt::Debug, ops::Range}; 2 2 3 - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 3 + #[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)] 4 4 /// Represents a range of values. 5 5 /// 6 6 /// End is exclusive. ··· 55 55 } 56 56 57 57 /// Merge this range with another. If the ranges do not intersect, [None] is returned. 58 + /// 59 + /// # Example 60 + /// 61 + /// ``` 62 + /// use utils::prelude::*; 63 + /// 64 + /// let a = BetterRange::new(1, 5); 65 + /// let b = BetterRange::new(6, 11); 66 + /// let c = BetterRange::new(4, 8); 67 + /// let d = BetterRange::new(3, 4); 68 + /// 69 + /// 70 + /// assert!(a.merge(&b).is_none(), "Should not merge"); 71 + /// assert!(d.merge(&b).is_none(), "Should not merge"); 72 + /// assert_eq!(a.merge(&c).unwrap(), BetterRange::new(1, 8), "1-5 & 4-8 = 1-8"); 73 + /// assert_eq!(c.merge(&a).unwrap(), BetterRange::new(1, 8), "4-8 & 1-5 = 1-8"); 74 + /// assert_eq!(a.merge(&d).unwrap(), a, "1-5 & 3-4 = 1-5"); 75 + /// assert_eq!(d.merge(&a).unwrap(), a, "3-4 & 1-5 = 1-5"); 76 + /// assert_eq!(c.merge(&d).unwrap(), BetterRange::new(3, 8), "4-8 & 3-4 = 3-8"); 77 + /// assert_eq!(d.merge(&c).unwrap(), BetterRange::new(3, 8), "3-4 & 4-8 = 3-8"); 78 + /// ``` 58 79 pub fn merge(&self, other: &Self) -> Option<Self> { 59 80 if self.contains_range(other) { 60 81 Some(*self) ··· 163 184 Self::new(self.start - rhs, self.end - rhs) 164 185 } 165 186 } 166 - 167 - impl<T: Copy + Clone + Debug + Ord> std::ops::BitAnd for BetterRange<T> 168 - where 169 - T: std::ops::BitAnd<Output = T>, 170 - { 171 - type Output = Self; 172 - 173 - fn bitand(self, rhs: Self) -> Self::Output { 174 - Self::new(self.start.max(rhs.start), self.end.min(rhs.end)) 175 - } 176 - } 177 - 178 - impl<T: Copy + Clone + Debug + Ord> std::ops::BitOr for BetterRange<T> 179 - where 180 - T: std::ops::BitOr<Output = T>, 181 - { 182 - type Output = Self; 183 - 184 - fn bitor(self, rhs: Self) -> Self::Output { 185 - Self::new(self.start.min(rhs.start), self.end.max(rhs.end)) 186 - } 187 - }
+1 -1
years/2025/src/day_5.rs
··· 16 16 } 17 17 18 18 fn part_2((mut ranges, _): Self::Input) -> Option<String> { 19 - ranges.sort_by(|a, b| a.start.cmp(&b.start).then(a.end.cmp(&b.end))); 19 + ranges.sort(); 20 20 21 21 let mut new_ranges = Vec::with_capacity(ranges.len()); 22 22