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