39 lines
1.2 KiB
Rust
39 lines
1.2 KiB
Rust
use regex::Regex;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Assignment {
|
|
pub lower_bound: u32,
|
|
pub upper_bound: u32,
|
|
}
|
|
|
|
impl Assignment {
|
|
pub fn contains(&self, other: &Self) -> bool {
|
|
self.lower_bound <= other.lower_bound && self.upper_bound >= other.upper_bound
|
|
}
|
|
|
|
pub fn overlaps(&self, other: &Self) -> bool {
|
|
self.lower_bound <= other.upper_bound && self.upper_bound >= other.lower_bound
|
|
}
|
|
}
|
|
|
|
pub fn parse_assignments(input: &String) -> Vec<(Assignment, Assignment)> {
|
|
let re = Regex::new(r"(\d+)-(\d+),(\d+)-(\d+)").unwrap();
|
|
let mut assignments = Vec::new();
|
|
|
|
for line in input.lines() {
|
|
assert!(re.is_match(line));
|
|
let captures = re.captures(line).unwrap();
|
|
let ass1 = Assignment {
|
|
lower_bound: captures.get(1).unwrap().as_str().parse().unwrap(),
|
|
upper_bound: captures.get(2).unwrap().as_str().parse().unwrap(),
|
|
};
|
|
let ass2 = Assignment {
|
|
lower_bound: captures.get(3).unwrap().as_str().parse().unwrap(),
|
|
upper_bound: captures.get(4).unwrap().as_str().parse().unwrap(),
|
|
};
|
|
assignments.push((ass1, ass2));
|
|
}
|
|
|
|
assignments
|
|
}
|