day 7 optimizations
- no need to sort thanks to `select_nth_unstable()` - don't copy the array around
This commit is contained in:
1 file changed
+14
-14
+14
-14
@@ -2,10 +2,10 @@ use anyhow::Result;
|
|||||||
|
|
||||||
use crate::utils::parse_by_char;
|
use crate::utils::parse_by_char;
|
||||||
|
|
||||||
fn get_median(positions: &[isize]) -> isize {
|
fn get_median(positions: &mut [isize]) -> isize {
|
||||||
let mut sorted_pos = positions.to_owned();
|
let median_index = positions.len() / 2;
|
||||||
sorted_pos.sort_unstable();
|
positions.select_nth_unstable(median_index); // quickselect in the standard library!
|
||||||
sorted_pos[sorted_pos.len() / 2]
|
positions[median_index]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compute_fuel(positions: &[isize], target: isize, cheap_fuel: bool) -> usize {
|
fn compute_fuel(positions: &[isize], target: isize, cheap_fuel: bool) -> usize {
|
||||||
@@ -22,7 +22,7 @@ fn compute_fuel(positions: &[isize], target: isize, cheap_fuel: bool) -> usize {
|
|||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_cheapest_location(positions: &[isize], cheap_fuel: bool) -> usize {
|
fn find_cheapest_location(positions: &mut [isize], cheap_fuel: bool) -> usize {
|
||||||
// find start point
|
// find start point
|
||||||
let start = get_median(positions);
|
let start = get_median(positions);
|
||||||
let mut position = start;
|
let mut position = start;
|
||||||
@@ -56,10 +56,10 @@ fn find_cheapest_location(positions: &[isize], cheap_fuel: bool) -> usize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn run() -> Result<(usize, usize)> {
|
pub(crate) fn run() -> Result<(usize, usize)> {
|
||||||
let data = parse_by_char::<isize>(include_str!("../data/day07.txt"), ',').unwrap();
|
let mut data = parse_by_char::<isize>(include_str!("../data/day07.txt"), ',').unwrap();
|
||||||
Ok((
|
Ok((
|
||||||
find_cheapest_location(&data, true),
|
find_cheapest_location(&mut data, true),
|
||||||
find_cheapest_location(&data, false),
|
find_cheapest_location(&mut data, false),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,15 +69,15 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_example() {
|
fn test_example() {
|
||||||
let data = parse_by_char::<isize>(include_str!("../data/day07-test.txt"), ',').unwrap();
|
let mut data = parse_by_char::<isize>(include_str!("../data/day07-test.txt"), ',').unwrap();
|
||||||
assert_eq!(find_cheapest_location(&data, true), 37);
|
assert_eq!(find_cheapest_location(&mut data, true), 37);
|
||||||
assert_eq!(find_cheapest_location(&data, false), 168);
|
assert_eq!(find_cheapest_location(&mut data, false), 168);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_my_data() {
|
fn test_my_data() {
|
||||||
let data = parse_by_char::<isize>(include_str!("../data/day07.txt"), ',').unwrap();
|
let mut data = parse_by_char::<isize>(include_str!("../data/day07.txt"), ',').unwrap();
|
||||||
assert_eq!(find_cheapest_location(&data, true), 344138);
|
assert_eq!(find_cheapest_location(&mut data, true), 344138);
|
||||||
assert_eq!(find_cheapest_location(&data, false), 94862124);
|
assert_eq!(find_cheapest_location(&mut data, false), 94862124);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user