diff --git a/2022/day04-part1/src/main.rs b/2022/day04-part1/src/main.rs index bd3b249..4019a26 100644 --- a/2022/day04-part1/src/main.rs +++ b/2022/day04-part1/src/main.rs @@ -19,20 +19,28 @@ fn main() { // Turn "1-2,3-6" into ["1-2", "3-6"]. let ranges = line.split(',').collect::>(); // Turn ["1-2", "3-6"] into [["1", "2"], ["3", "6"]] - let ranges = ranges.iter().map(|x| x.split('-').collect::>()).collect::>(); + let ranges = ranges + .iter() + .map(|x| x.split('-').collect::>()) + .collect::>(); // Turn [["1", "2"], ["3", "6"]] into [1..=2, 3..=6] - let ranges = ranges.iter().map(|x| (x[0].parse::().unwrap())..=(x[1].parse::().unwrap())).collect::>(); + let ranges = ranges + .iter() + .map(|x| (x[0].parse::().unwrap())..=(x[1].parse::().unwrap())) + .collect::>(); // Now turn the ranges into HashSets containing the respective integers. Essentially: // Turn [1..=2, 3..=6] into [{1,2}, {3,4,5,6}] - let sets = ranges.into_iter().map(|x| x.collect::>()).collect::>(); + let sets = ranges + .into_iter() + .map(|x| x.collect::>()) + .collect::>(); + // Check if either of the sets is a subset of the other, i.e. is fully contained in the other. + // If so, add it to the count. if sets[0].is_subset(&sets[1]) || sets[1].is_subset(&sets[0]) { count += 1; } - - } println!("Count: {}", count); } -