2022 day13/rust: clean up
This commit is contained in:
parent
f08a4aa274
commit
4022aa6e68
1 changed files with 13 additions and 13 deletions
|
@ -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>()
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue