From 028da62e257ba5587adf17350208c0b461a20d4b Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 5 Dec 2022 12:33:43 +0100 Subject: [PATCH] Solve Part 2 --- src/main.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3574418..90e5250 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ fn parse_range(string: &str) -> Range 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() }