Solve Part 2

This commit is contained in:
apio 2022-12-05 12:33:43 +01:00
parent a83326d9c4
commit 028da62e25
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -11,6 +11,7 @@ fn parse_range(string: &str) -> Range<i32>
left.parse().unwrap() .. right.parse().unwrap() left.parse().unwrap() .. right.parse().unwrap()
} }
#[allow(dead_code)]
fn part1() fn part1()
{ {
let mut pairs_fully_contained = 0; 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); println!("There are {} pairs in which one range is fully contained in the other one", pairs_fully_contained);
} }
fn main() { fn part2()
part1() {
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()
} }