Skip to content

Commit

Permalink
refactor day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
MellKam committed Dec 5, 2022
1 parent 80be819 commit 89ea732
Showing 1 changed file with 58 additions and 54 deletions.
112 changes: 58 additions & 54 deletions day_4/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,79 +1,83 @@
use std::{fmt::Debug, ops::Range, str::FromStr};

fn parse_to_range<T: Sized + FromStr>(string: &str) -> Range<T>
where
T::Err: Debug,
{
let (start, end) = match string.split_once("-") {
Some((s, e)) => (s.parse::<T>().unwrap(), e.parse::<T>().unwrap()),
_ => panic!("Cannot parse string to range"),
};

return Range { start, end };
}

#[derive(Debug)]
struct Range {
start: i32,
end: i32,
struct RangePair<T: Sized> {
r1: Range<T>,
r2: Range<T>,
}

impl From<&str> for Range {
impl<T: Sized + FromStr> From<&str> for RangePair<T>
where
T::Err: Debug,
{
fn from(string: &str) -> Self {
let (start, end) = match string.split_once("-") {
Some((s, e)) => (s.parse::<i32>().unwrap(), e.parse::<i32>().unwrap()),
_ => panic!(),
let (r1, r2) = match string.split_once(",") {
Some((a, b)) => (parse_to_range::<T>(a), parse_to_range::<T>(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<T: PartialOrd> RangePair<T> {
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::<u32>::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::<u32>::from(line).overlap() as u32)
.sum();

println!("{result}");
println!("{result2}");
println!("{contains}");
println!("{overlaps}");
}

0 comments on commit 89ea732

Please sign in to comment.