Speed up day9
This commit is contained in:
parent
05b76ae365
commit
0b831d59c9
@ -24,20 +24,18 @@ fn checksum(filesystem: &[isize]) -> usize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn part1(filesystem: &mut [isize]) -> usize {
|
fn part1(filesystem: &mut [isize]) -> usize {
|
||||||
|
let mut last = filesystem.len() - 1;
|
||||||
for space_idx in 0..filesystem.len() {
|
for space_idx in 0..filesystem.len() {
|
||||||
if filesystem[space_idx] >= 0 {
|
if filesystem[space_idx] >= 0 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let last = filesystem
|
while filesystem[last] < 0 {
|
||||||
.iter()
|
last -= 1;
|
||||||
.enumerate()
|
}
|
||||||
.rev()
|
if last < space_idx {
|
||||||
.find(|(_, n)| n >= &&0)
|
|
||||||
.unwrap();
|
|
||||||
if last.0 < space_idx {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
filesystem.swap(space_idx, last.0);
|
filesystem.swap(space_idx, last);
|
||||||
}
|
}
|
||||||
checksum(filesystem)
|
checksum(filesystem)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user