Skip to content

Commit

Permalink
advent_of_code 2022-4: part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
msyfls123 committed May 25, 2023
1 parent f74cc0a commit 769c833
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions advent_of_code/src/aoc2022/4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ impl Pair {
let (s3, s4) = self.section2;
(s1 <= s3 && s2 >= s4) || (s1 >= s3 && s2 <= s4)
}

fn is_overlapped(&self) -> bool {
if self.is_contained() {
return true;
}
let (s1, s2) = self.section1;
let (s3, s4) = self.section2;
(s1 <= s3 && s3 <= s2) || (s1 <= s4 && s4 <= s2)
}
}

fn get_pair(text: String) -> Pair {
Expand All @@ -42,4 +51,6 @@ fn main() {
let pairs: Vec<Pair> = data.iter().map(|line| get_pair(line.to_string())).collect();
let contained_pairs = pairs.iter().filter(|p| p.is_contained()).count();
println!("Part 1: {}", contained_pairs);
let overlapped_pairs = pairs.iter().filter(|p| p.is_overlapped()).count();
println!("Part 2: {}", overlapped_pairs);
}

0 comments on commit 769c833

Please sign in to comment.