// 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 . use std::io; use std::ops::RangeInclusive; pub struct Assignments {} #[derive(Debug)] pub struct Assignment { range1: RangeInclusive, range2: RangeInclusive, } impl Iterator for Assignments { type Item = Result; fn next(&mut self) -> Option { 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, range2: RangeInclusive) -> Self { Self { range1: range1, range2: range2, } } fn parse_range(input: &str) -> Result, &'static str> { if let Some((start, end)) = input.split_once('-') { if let (Ok(start), Ok(end)) = (start.parse::(), end.parse::()) { return Ok(start..=end); } } Err("invalid range string") } pub fn from_str(input: &str) -> Result { 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()) } }