Reformat code for 2022/day04-part1

This commit is contained in:
Tobias Marschner 2024-02-11 06:10:23 +01:00
parent 47eaa0bd32
commit 1802a8f6ae

View File

@ -19,20 +19,28 @@ fn main() {
// Turn "1-2,3-6" into ["1-2", "3-6"].
let ranges = line.split(',').collect::<Vec<_>>();
// Turn ["1-2", "3-6"] into [["1", "2"], ["3", "6"]]
let ranges = ranges.iter().map(|x| x.split('-').collect::<Vec<_>>()).collect::<Vec<_>>();
let ranges = ranges
.iter()
.map(|x| x.split('-').collect::<Vec<_>>())
.collect::<Vec<_>>();
// Turn [["1", "2"], ["3", "6"]] into [1..=2, 3..=6]
let ranges = ranges.iter().map(|x| (x[0].parse::<i32>().unwrap())..=(x[1].parse::<i32>().unwrap())).collect::<Vec<_>>();
let ranges = ranges
.iter()
.map(|x| (x[0].parse::<i32>().unwrap())..=(x[1].parse::<i32>().unwrap()))
.collect::<Vec<_>>();
// 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::<HashSet<_>>()).collect::<Vec<_>>();
let sets = ranges
.into_iter()
.map(|x| x.collect::<HashSet<_>>())
.collect::<Vec<_>>();
// 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);
}