use itertools::Itertools; #[derive(Debug)] struct Elf { start: usize, end: usize, } #[derive(Debug)] struct Pair(Elf, Elf); impl Elf { pub fn new(range: &str) -> Self { if let Some((start, end)) = range.to_string().split("-").collect_tuple() { let start: usize = start.parse().unwrap(); let end: usize = end.parse().unwrap(); Self { start, end } } else { panic!("Expected two elements"); } } } impl Pair { pub fn new(line: &str) -> Self { if let Some((first, second)) = line.to_string().split(",").collect_tuple() { Pair(Elf::new(first), Elf::new(second)) } else { panic!("Expected two elements"); } } } impl Pair { fn contains(&self) -> bool { return (self.0.start <= self.1.start && self.0.end >= self.1.end) || (self.0.start >= self.1.start && self.0.end <= self.1.end); } } fn main() { let sum: usize = include_str!("../../example") .lines() .map(|l| Pair::new(l)) .map(|p| p.contains()) .filter(|r| *r) .count(); println!("{}", sum); }