42 lines
1.4 KiB
Rust
42 lines
1.4 KiB
Rust
fn mutual_inclusion(first_pair: &[u32], second_pair: &[u32]) -> bool {
|
|
(first_pair[0] <= second_pair[0] && first_pair[1] >= second_pair[1])
|
|
|| (second_pair[0] <= first_pair[0] && second_pair[1] >= first_pair[1])
|
|
}
|
|
|
|
pub fn solve_part1(file_path: &str) -> i32 {
|
|
std::fs::read_to_string(file_path)
|
|
.expect("Please provide a text file as an argument.")
|
|
.lines()
|
|
.map(|line| {
|
|
let tmp = line.split(',').collect::<Vec<&str>>();
|
|
let firs_pair = tmp
|
|
.first()
|
|
.expect("First character error")
|
|
.split('-')
|
|
.map(|first_split| {
|
|
first_split
|
|
.parse::<u32>()
|
|
.expect("Parsing number in first pair failed")
|
|
})
|
|
.collect::<Vec<u32>>();
|
|
let second_pair = tmp
|
|
.last()
|
|
.expect("First character error")
|
|
.split('-')
|
|
.map(|first_split| {
|
|
first_split
|
|
.parse::<u32>()
|
|
.expect("Parsing number in first pair failed")
|
|
})
|
|
.collect::<Vec<u32>>();
|
|
if mutual_inclusion(&firs_pair, &second_pair) {
|
|
1
|
|
} else {
|
|
0
|
|
}
|
|
})
|
|
.collect::<Vec<i32>>()
|
|
.iter()
|
|
.sum()
|
|
}
|