tangled
alpha
login
or
join now
bwc9876.dev
/
advent
0
fork
atom
Advent of Code solutions
0
fork
atom
overview
issues
pulls
pipelines
Doctests for BetterRange::merge
bwc9876.dev
3 months ago
d031c0be
796138a9
verified
This commit was signed with the committer's
known signature
.
bwc9876.dev
SSH Key Fingerprint:
SHA256:DanMEP/RNlSC7pAVbnXO6wzQV00rqyKj053tz4uH5gQ=
+23
-24
2 changed files
expand all
collapse all
unified
split
utils
src
range.rs
years
2025
src
day_5.rs
+22
-23
utils/src/range.rs
···
1
1
use std::{fmt::Debug, ops::Range};
2
2
3
3
-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3
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
58
+
///
59
59
+
/// # Example
60
60
+
///
61
61
+
/// ```
62
62
+
/// use utils::prelude::*;
63
63
+
///
64
64
+
/// let a = BetterRange::new(1, 5);
65
65
+
/// let b = BetterRange::new(6, 11);
66
66
+
/// let c = BetterRange::new(4, 8);
67
67
+
/// let d = BetterRange::new(3, 4);
68
68
+
///
69
69
+
///
70
70
+
/// assert!(a.merge(&b).is_none(), "Should not merge");
71
71
+
/// assert!(d.merge(&b).is_none(), "Should not merge");
72
72
+
/// assert_eq!(a.merge(&c).unwrap(), BetterRange::new(1, 8), "1-5 & 4-8 = 1-8");
73
73
+
/// assert_eq!(c.merge(&a).unwrap(), BetterRange::new(1, 8), "4-8 & 1-5 = 1-8");
74
74
+
/// assert_eq!(a.merge(&d).unwrap(), a, "1-5 & 3-4 = 1-5");
75
75
+
/// assert_eq!(d.merge(&a).unwrap(), a, "3-4 & 1-5 = 1-5");
76
76
+
/// assert_eq!(c.merge(&d).unwrap(), BetterRange::new(3, 8), "4-8 & 3-4 = 3-8");
77
77
+
/// assert_eq!(d.merge(&c).unwrap(), BetterRange::new(3, 8), "3-4 & 4-8 = 3-8");
78
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
166
-
167
167
-
impl<T: Copy + Clone + Debug + Ord> std::ops::BitAnd for BetterRange<T>
168
168
-
where
169
169
-
T: std::ops::BitAnd<Output = T>,
170
170
-
{
171
171
-
type Output = Self;
172
172
-
173
173
-
fn bitand(self, rhs: Self) -> Self::Output {
174
174
-
Self::new(self.start.max(rhs.start), self.end.min(rhs.end))
175
175
-
}
176
176
-
}
177
177
-
178
178
-
impl<T: Copy + Clone + Debug + Ord> std::ops::BitOr for BetterRange<T>
179
179
-
where
180
180
-
T: std::ops::BitOr<Output = T>,
181
181
-
{
182
182
-
type Output = Self;
183
183
-
184
184
-
fn bitor(self, rhs: Self) -> Self::Output {
185
185
-
Self::new(self.start.min(rhs.start), self.end.max(rhs.end))
186
186
-
}
187
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
19
-
ranges.sort_by(|a, b| a.start.cmp(&b.start).then(a.end.cmp(&b.end)));
19
19
+
ranges.sort();
20
20
21
21
let mut new_ranges = Vec::with_capacity(ranges.len());
22
22