Drop useless priority queue in day12

This commit is contained in:
Malte Tammena 2022-12-13 14:27:28 +01:00
parent d3696afdfa
commit fad59a04ce
3 changed files with 17 additions and 76 deletions

54
Cargo.lock generated
View file

@ -2,29 +2,12 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "ahash"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf6ccdb167abbf410dcb915cabd428929d7f6a04980b54a11f26a39f1c7f7107"
dependencies = [
"cfg-if",
"once_cell",
"version_check",
]
[[package]]
name = "arrays"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dbdbb2877d26e0647c6b8125802b2ecf2dc2a28d864dde41e6f9b9a54da08fe"
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "beef"
version = "0.5.2"
@ -175,8 +158,6 @@ name = "day12"
version = "0.1.0"
dependencies = [
"colorous",
"hashbrown 0.13.1",
"priority-queue",
"termion",
]
@ -273,21 +254,6 @@ dependencies = [
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hashbrown"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33ff8ae62cd3a9102e5637afc8452c55acf3844001bd5374e0b0bd7b6616c038"
dependencies = [
"ahash",
]
[[package]]
name = "heck"
version = "0.4.0"
@ -303,16 +269,6 @@ dependencies = [
"libc",
]
[[package]]
name = "indexmap"
version = "1.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399"
dependencies = [
"autocfg",
"hashbrown 0.12.3",
]
[[package]]
name = "io-lifetimes"
version = "1.0.3"
@ -429,16 +385,6 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "priority-queue"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7685ca4cc0b3ad748c22ce6803e23b55b9206ef7715b965ebeaf41639238fdc"
dependencies = [
"autocfg",
"indexmap",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"

View file

@ -9,6 +9,4 @@ app = true
[dependencies]
colorous = "1.0.8"
hashbrown = "0.13.1"
priority-queue = "1.3.0"
termion = "2.0.1"

View file

@ -1,7 +1,4 @@
use std::{cmp::Reverse, io::Read};
use hashbrown::hash_map::DefaultHashBuilder;
use priority_queue::PriorityQueue;
use std::{collections::VecDeque, io::Read};
#[derive(Debug)]
pub struct HeightMap {
@ -12,9 +9,8 @@ pub struct HeightMap {
impl HeightMap {
pub fn find_shortest_path_len_to_end<const TARGET: u8>(&self) -> usize {
let mut visited: Vec<bool> = self.heights.iter().map(|_| false).collect();
let mut open: PriorityQueue<usize, Reverse<u32>, DefaultHashBuilder> =
PriorityQueue::with_hasher(DefaultHashBuilder::default());
let mut queued: Vec<bool> = self.heights.iter().map(|_| false).collect();
let mut open: VecDeque<(usize, u32)> = VecDeque::new();
let end_idx = self
.heights
.iter()
@ -22,16 +18,17 @@ impl HeightMap {
.find(|(_, &height)| height == b'E')
.unwrap()
.0;
open.push(end_idx, Reverse(0));
while let Some((current_idx, Reverse(current_score))) = open.pop() {
open.push_back((end_idx, 0));
queued[end_idx] = true;
while let Some((current_idx, current_score)) = open.pop_front() {
if self.heights[current_idx] == TARGET {
return current_score as usize;
}
visited[current_idx] = true;
for neighbor_idx in self.neighbors_of(current_idx) {
let tentative_score = current_score + 1;
if !visited[neighbor_idx] {
open.push(neighbor_idx, Reverse(tentative_score));
if !queued[neighbor_idx] {
queued[neighbor_idx] = true;
open.push_back((neighbor_idx, tentative_score));
}
}
}
@ -39,10 +36,9 @@ impl HeightMap {
}
pub fn find_shortest_path_to_end<const TARGET: u8>(&self) -> Vec<usize> {
let mut visited: Vec<bool> = self.heights.iter().map(|_| false).collect();
let mut queued: Vec<bool> = self.heights.iter().map(|_| false).collect();
let mut came_from: Vec<Option<usize>> = self.heights.iter().map(|_| None).collect();
let mut open: PriorityQueue<usize, Reverse<u32>, DefaultHashBuilder> =
PriorityQueue::with_hasher(DefaultHashBuilder::default());
let mut open: VecDeque<(usize, u32)> = VecDeque::new();
let end_idx = self
.heights
.iter()
@ -50,8 +46,9 @@ impl HeightMap {
.find(|(_, &height)| height == b'E')
.unwrap()
.0;
open.push(end_idx, Reverse(0));
while let Some((current_idx, Reverse(current_score))) = open.pop() {
open.push_back((end_idx, 0));
queued[end_idx] = true;
while let Some((current_idx, current_score)) = open.pop_front() {
if self.heights[current_idx] == TARGET {
let mut current = current_idx;
let mut path = vec![current];
@ -62,12 +59,12 @@ impl HeightMap {
path.reverse();
return path;
}
visited[current_idx] = true;
for neighbor_idx in self.neighbors_of(current_idx) {
let tentative_score = current_score + 1;
if !visited[neighbor_idx] {
if !queued[neighbor_idx] {
came_from[neighbor_idx] = Some(current_idx);
open.push(neighbor_idx, Reverse(tentative_score));
queued[neighbor_idx] = true;
open.push_back((neighbor_idx, tentative_score));
}
}
}