From 89ea7328aeb3cb4bd8f3aae3d1998f402827d9f7 Mon Sep 17 00:00:00 2001 From: MelKam Date: Mon, 5 Dec 2022 10:53:59 +0000 Subject: [PATCH] refactor day 4 --- day_4/rust/src/main.rs | 112 +++++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 54 deletions(-) diff --git a/day_4/rust/src/main.rs b/day_4/rust/src/main.rs index 0c8cada..b84f847 100644 --- a/day_4/rust/src/main.rs +++ b/day_4/rust/src/main.rs @@ -1,79 +1,83 @@ +use std::{fmt::Debug, ops::Range, str::FromStr}; + +fn parse_to_range(string: &str) -> Range +where + T::Err: Debug, +{ + let (start, end) = match string.split_once("-") { + Some((s, e)) => (s.parse::().unwrap(), e.parse::().unwrap()), + _ => panic!("Cannot parse string to range"), + }; + + return Range { start, end }; +} + #[derive(Debug)] -struct Range { - start: i32, - end: i32, +struct RangePair { + r1: Range, + r2: Range, } -impl From<&str> for Range { +impl From<&str> for RangePair +where + T::Err: Debug, +{ fn from(string: &str) -> Self { - let (start, end) = match string.split_once("-") { - Some((s, e)) => (s.parse::().unwrap(), e.parse::().unwrap()), - _ => panic!(), + let (r1, r2) = match string.split_once(",") { + Some((a, b)) => (parse_to_range::(a), parse_to_range::(b)), + _ => panic!("Cannot split string"), }; - return Self { start, end }; + Self { r1, r2 } } } -fn ranges_fully_contain(range1: &Range, range2: &Range) -> bool { - if range1.start == range2.start || range1.end == range2.end { - return true; - } - if range1.start < range2.start && range1.end > range2.end { - return true; - } - if range2.end > range1.end { - return true; +impl RangePair { + fn contain(&self) -> bool { + if self.r1.start <= self.r2.start && self.r1.end >= self.r2.end { + // then r1 is outer range and r2 is inner + return true; + } + if self.r2.start <= self.r1.start && self.r2.end >= self.r1.end { + // then r2 is outer range and r1 in inner + return true; + } + + return false; } - return false; -} + fn overlap(&self) -> bool { + if self.r1.start > self.r2.start && self.r2.end > self.r1.start { + return true; + } + if self.r1.start < self.r2.start && self.r1.end > self.r2.start { + return true; + } + if self.r1.start == self.r2.start + || self.r1.end == self.r2.end + || self.r1.start == self.r2.end + || self.r2.start == self.r1.end + { + return true; + } -fn ranges_overlap(range1: &Range, range2: &Range) -> bool { - if range1.start == range2.start - || range1.end == range2.end - || range1.start == range2.end - || range2.start == range1.end - { - return true; - } - if range1.start > range2.start && range2.end > range1.start { - return true; - } - if range1.start < range2.start && range1.end > range2.start { - return true; + return false; } - - return false; } fn main() { let data = include_str!("../../input.txt"); - let result: i32 = data + let contains: u32 = data .lines() - .map(|line| { - let (range1, range2) = match line.split_once(",") { - Some((a, b)) => (Range::from(a), Range::from(b)), - _ => panic!(), - }; - - return ranges_fully_contain(&range1, &range2) as i32; - }) + .map(|line| RangePair::::from(line).contain() as u32) .sum(); - let result2: i32 = data + let overlaps: u32 = data .lines() - .map(|line| { - let (range1, range2) = match line.split_once(",") { - Some((a, b)) => (Range::from(a), Range::from(b)), - _ => panic!(), - }; - - return ranges_overlap(&range1, &range2) as i32; - }) + .map(|line| RangePair::::from(line).overlap() as u32) .sum(); - println!("{result}"); - println!("{result2}"); + println!("{contains}"); + println!("{overlaps}"); }