aoc22/src/day4.rs

39 lines
1.2 KiB
Rust
Raw Normal View History

2022-12-13 15:37:46 +01:00
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
}
2022-12-13 15:38:10 +01:00
pub fn overlaps(&self, other: &Self) -> bool {
self.lower_bound <= other.upper_bound && self.upper_bound >= other.lower_bound
}
2022-12-13 15:37:46 +01:00
}
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
}