Solve Part 2
This commit is contained in:
parent
a83326d9c4
commit
028da62e25
26
src/main.rs
26
src/main.rs
@ -11,6 +11,7 @@ fn parse_range(string: &str) -> Range<i32>
|
||||
left.parse().unwrap() .. right.parse().unwrap()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn part1()
|
||||
{
|
||||
let mut pairs_fully_contained = 0;
|
||||
@ -34,6 +35,27 @@ fn part1()
|
||||
println!("There are {} pairs in which one range is fully contained in the other one", pairs_fully_contained);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
part1()
|
||||
fn part2()
|
||||
{
|
||||
let mut overlapping_pairs = 0;
|
||||
for slice in DATA.split("\n")
|
||||
{
|
||||
if slice.is_empty() { continue; }
|
||||
|
||||
let (left, right) = slice.split_once(",").expect("Input data is not correctly structured");
|
||||
|
||||
let left_range = parse_range(left);
|
||||
let right_range = parse_range(right);
|
||||
|
||||
if left_range.start <= right_range.end && right_range.start <= left_range.end
|
||||
{
|
||||
overlapping_pairs += 1;
|
||||
}
|
||||
}
|
||||
println!("There are {} pairs where ranges overlap", overlapping_pairs);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
//part1()
|
||||
part2()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user