40 lines
1.5 KiB
Rust
40 lines
1.5 KiB
Rust
use anyhow::Result;
|
|
use std::fs;
|
|
|
|
pub fn run() -> Result<()> {
|
|
let file_contents = fs::read_to_string("day4.txt")?;
|
|
let lines = file_contents.lines();
|
|
let mut part_one_pairs = 0;
|
|
let mut part_two_pairs = 0;
|
|
|
|
for line in lines {
|
|
let v: Vec<&str> = line.split(",").collect();
|
|
let left: Vec<u32> = v[0].split("-").map(|t| t.parse::<u32>().unwrap()).collect();
|
|
let right: Vec<u32> = v[1].split("-").map(|t| t.parse::<u32>().unwrap()).collect();
|
|
|
|
let left_outside = left[0] <= right[0] && left[1] >= right[1];
|
|
let left_inside = left[0] >= right[0] && left[1] <= right[1];
|
|
let left_covered = left_outside || left_inside;
|
|
|
|
let right_outside = right[0] <= left[0] && right[1] >= left[1];
|
|
let right_inside = right[0] >= left[0] && right[1] <= left[1];
|
|
let right_covered = right_outside || right_inside;
|
|
|
|
if left_covered && right_covered {
|
|
part_one_pairs += 1;
|
|
}
|
|
|
|
let left_1_inside = left[0] >= right[0] && left[0] <= right[1];
|
|
let left_2_inside = left[1] >= right[0] && left[1] <= right[1];
|
|
let right_1_inside = right[0] >= left[0] && right[0] <= left[1];
|
|
let right_2_inside = right[1] >= left[0] && right[1] <= left[1];
|
|
if left_1_inside || left_2_inside || right_1_inside || right_2_inside {
|
|
part_two_pairs += 1;
|
|
}
|
|
}
|
|
println!("part one pairs {}", part_one_pairs);
|
|
println!("part two pairs {}", part_two_pairs);
|
|
|
|
Ok(())
|
|
}
|