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();
|
.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));
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue