2022 day13/rust: clean up

This commit is contained in:
Xiretza 2022-12-13 18:51:33 +01:00
parent f08a4aa274
commit 4022aa6e68

View file

@ -50,29 +50,29 @@ fn main() {
})
.collect();
let sum: usize = pairs
let num_sorted: usize = pairs
.iter()
.enumerate()
.filter_map(|(i, (left, right))| (left <= right).then_some(i + 1))
.sum();
println!("{}", sum);
println!("{}", num_sorted);
let dividers = [2, 6]
.into_iter()
.map(|i| Data::List(vec![Data::List(vec![Data::Number(i)])]));
let mut sorted: Vec<_> = pairs
.into_iter()
.flat_map(|(left, right)| [left, right])
.collect();
let divider = |divider| Data::List(vec![Data::List(vec![Data::Number(divider)])]);
sorted.push(divider(2));
sorted.push(divider(6));
sorted.extend(dividers.clone());
sorted.sort();
let index = |divider| {
sorted
.binary_search(&Data::List(vec![Data::List(vec![Data::Number(divider)])]))
.unwrap()
+ 1
};
println!("{}", index(2) * index(6));
println!(
"{}",
dividers
.map(|div| sorted.binary_search(&div).unwrap() + 1)
.product::<usize>()
);
}