day 4 part 2
This commit is contained in:
parent
8717e1d31a
commit
0f2b0fc80e
18
four.rs
18
four.rs
|
@ -1,6 +1,7 @@
|
|||
fn main() {
|
||||
let buf = std::fs::read_to_string("four.txt").unwrap();
|
||||
let mut count = 0u32;
|
||||
let mut count_contains = 0u32;
|
||||
let mut count_overlaps = 0u32;
|
||||
for line in buf.lines() {
|
||||
let (first, last) = line.split_once(',').unwrap();
|
||||
let (first_lower, first_upper) = first
|
||||
|
@ -15,13 +16,24 @@ fn main() {
|
|||
if contains(first_lower, first_upper, last_lower, last_upper)
|
||||
|| contains(last_lower, last_upper, first_lower, first_upper)
|
||||
{
|
||||
count += 1;
|
||||
count_contains += 1;
|
||||
}
|
||||
|
||||
if overlaps(first_lower, first_upper, last_lower, last_upper)
|
||||
|| overlaps(last_lower, last_upper, first_lower, first_upper)
|
||||
{
|
||||
count_overlaps += 1;
|
||||
}
|
||||
}
|
||||
|
||||
println!("{}", count);
|
||||
println!("{}", count_contains);
|
||||
println!("{}", count_overlaps);
|
||||
}
|
||||
|
||||
fn contains(a: u32, b: u32, c: u32, d: u32) -> bool {
|
||||
return (a..=b).contains(&c) && (a..=b).contains(&d);
|
||||
}
|
||||
|
||||
fn overlaps(a: u32, b: u32, c: u32, d: u32) -> bool {
|
||||
return (a..=b).contains(&c) || (a..=b).contains(&d);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue