1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
use std::fs;
use std::io::{self, BufRead};
// fn subinterval(a0: i32, a1: i32, b0: i32, b1: i32) -> bool {
// a0 <= b0 && b1 <= a1
// }
fn subcontained(a0: i32, a1: i32, b0: i32, b1: i32) -> bool {
// subinterval(a0, a1, b0, b1) || subinterval(b0, b1, a0, a1)
((a0 - b0) * (a1 - b1)) <= 0
}
fn overlap(a0: i32, a1: i32, b0: i32, b1: i32) -> bool {
a0 <= b1 && b0 <= a1
}
fn main() {
let file = fs::File::open("input").unwrap();
let lines = io::BufReader::new(file).lines();
let mut subcont = 0;
let mut overlaps = 0;
for line in lines {
let li = line.expect("line");
if let [a0, a1, b0, b1] = li
.split(&[',', '-'])
.map(|x| x.parse::<i32>().unwrap())
.collect::<Vec<i32>>()[..4]
{
if subcontained(a0, a1, b0, b1) {
subcont += 1
}
if overlap(a0, a1, b0, b1) {
overlaps += 1
}
} else {
panic!("Bad line input")
};
}
assert_eq!(515, subcont);
assert_eq!(883, overlaps);
println!("All test passed.")
}
|