80 lines
2.5 KiB
Rust
80 lines
2.5 KiB
Rust
// Copyright 2022 Christian Ulrich
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
use std::io;
|
|
use std::ops::RangeInclusive;
|
|
|
|
pub struct Assignments {}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Assignment {
|
|
range1: RangeInclusive<u8>,
|
|
range2: RangeInclusive<u8>,
|
|
}
|
|
|
|
impl Iterator for Assignments {
|
|
type Item = Result<Assignment, &'static str>;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
if let Some(line) = io::stdin().lines().next() {
|
|
let line = line.unwrap();
|
|
if line.len() > 0 {
|
|
return Some(Assignment::from_str(&line));
|
|
}
|
|
}
|
|
None
|
|
}
|
|
}
|
|
|
|
impl Assignment {
|
|
pub fn new(range1: RangeInclusive<u8>, range2: RangeInclusive<u8>) -> Self {
|
|
Self {
|
|
range1: range1,
|
|
range2: range2,
|
|
}
|
|
}
|
|
|
|
fn parse_range(input: &str) -> Result<RangeInclusive<u8>, &'static str> {
|
|
if let Some((start, end)) = input.split_once('-') {
|
|
if let (Ok(start), Ok(end)) = (start.parse::<u8>(), end.parse::<u8>()) {
|
|
return Ok(start..=end);
|
|
}
|
|
}
|
|
Err("invalid range string")
|
|
}
|
|
|
|
pub fn from_str(input: &str) -> Result<Self, &'static str> {
|
|
if let Some((left, right)) = input.split_once(',') {
|
|
return Ok(Assignment::new(
|
|
Self::parse_range(left)?,
|
|
Self::parse_range(right)?,
|
|
));
|
|
}
|
|
Err("invalid assignment string")
|
|
}
|
|
|
|
pub fn has_obsolete_range(&self) -> bool {
|
|
self.range1.contains(self.range2.start()) && self.range1.contains(self.range2.end())
|
|
|| self.range2.contains(self.range1.start()) && self.range2.contains(self.range1.end())
|
|
}
|
|
|
|
pub fn has_overlapping_ranges(&self) -> bool {
|
|
self.range1.contains(self.range2.start())
|
|
|| self.range1.contains(self.range2.end())
|
|
|| self.range2.contains(self.range1.start())
|
|
|| self.range2.contains(self.range1.end())
|
|
}
|
|
}
|