26 lines
953 B
Rust
26 lines
953 B
Rust
use std::error;
|
|
use std::fs::File;
|
|
use std::io::BufRead;
|
|
use std::io::BufReader;
|
|
use std::ops;
|
|
|
|
fn main() -> Result<(), Box<dyn error::Error>> {
|
|
let f = File::open("input.txt")?;
|
|
let reader = BufReader::new(f);
|
|
|
|
let mut count: u32 = 0;
|
|
for line in reader.lines() {
|
|
let line = line?;
|
|
let range_strs = line.split(",").collect::<Vec<&str>>();
|
|
let range_a_u32 = range_strs[0].split("-").map(|x| x.parse::<u32>().unwrap()).collect::<Vec<u32>>();
|
|
let range_a = ops::Range { start: range_a_u32[0], end: range_a_u32[1] + 1 };
|
|
let range_b_u32 = range_strs[1].split("-").map(|x| x.parse::<u32>().unwrap()).collect::<Vec<u32>>();
|
|
let range_b = ops::Range { start: range_b_u32[0], end: range_b_u32[1] + 1 };
|
|
if range_a.contains(&range_b.start) || range_a.contains(&(range_b.end - 1)) || range_b.contains(&range_a.start) || range_b.contains(&(range_a.end - 1)) {
|
|
count += 1;
|
|
}
|
|
}
|
|
println!("Found {} overlapping ranges.", count);
|
|
Ok(())
|
|
}
|