You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.7 KiB
Rust
60 lines
1.7 KiB
Rust
use std::io::{self, BufRead};
|
|
use std::num::ParseIntError;
|
|
use std::str::FromStr;
|
|
|
|
#[derive(Debug)]
|
|
struct Pair {
|
|
first: (usize, usize),
|
|
second: (usize, usize),
|
|
}
|
|
|
|
fn main() {
|
|
let pairs = io::stdin()
|
|
.lock()
|
|
.lines()
|
|
.map(|l| Pair::from_str(l.unwrap().as_str()).unwrap());
|
|
|
|
let mut full_overlap = 0;
|
|
let mut partial_overlap = 0;
|
|
|
|
for pair in pairs {
|
|
if (pair.first.0 >= pair.second.0 && pair.first.1 <= pair.second.1)
|
|
|| (pair.first.0 <= pair.second.0 && pair.first.1 >= pair.second.1)
|
|
{
|
|
full_overlap += 1;
|
|
}
|
|
|
|
if (pair.first.0 >= pair.second.0 && pair.first.0 <= pair.second.1)
|
|
|| (pair.first.1 >= pair.second.0 && pair.first.1 <= pair.second.1)
|
|
|| (pair.second.0 >= pair.first.0 && pair.second.0 <= pair.first.1)
|
|
|| (pair.second.1 >= pair.first.0 && pair.second.1 <= pair.first.1)
|
|
{
|
|
partial_overlap += 1;
|
|
}
|
|
}
|
|
|
|
println!("P1: {}, P2: {}", full_overlap, partial_overlap);
|
|
}
|
|
|
|
impl FromStr for Pair {
|
|
type Err = ParseIntError;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
let (first, second) = s.split_once(',').unwrap();
|
|
let first_half = first.split_once('-').unwrap();
|
|
let first_half = (
|
|
first_half.0.parse::<usize>().unwrap(),
|
|
first_half.1.parse::<usize>().unwrap(),
|
|
);
|
|
let second_half = second.split_once('-').unwrap();
|
|
let second_half = (
|
|
second_half.0.parse::<usize>().unwrap(),
|
|
second_half.1.parse::<usize>().unwrap(),
|
|
);
|
|
Ok(Pair {
|
|
first: first_half,
|
|
second: second_half,
|
|
})
|
|
}
|
|
}
|