little cleaning of day 18
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
mod tasks;
|
mod tasks;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
tasks::day24::run();
|
tasks::day18::run();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ pub fn run() {
|
|||||||
|
|
||||||
let t1 = task(input.clone());
|
let t1 = task(input.clone());
|
||||||
println!("Task 1: best bound to get all keys is {}", t1);
|
println!("Task 1: best bound to get all keys is {}", t1);
|
||||||
let t2 = task(input.clone().split_robot());
|
//let t2 = task(input.clone().split_robot());
|
||||||
println!("Task 2: best bound to get all keys is {}", t2);
|
//println!("Task 2: best bound to get all keys is {}", t2);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn task(map: Map) -> usize {
|
fn task(map: Map) -> usize {
|
||||||
@@ -32,20 +32,17 @@ fn task(map: Map) -> usize {
|
|||||||
|
|
||||||
let mut visited: HashSet<StateSummary> = HashSet::new();
|
let mut visited: HashSet<StateSummary> = HashSet::new();
|
||||||
let mut open: BinaryHeap<State> = BinaryHeap::new();
|
let mut open: BinaryHeap<State> = BinaryHeap::new();
|
||||||
let mut best_found_for_all = std::usize::MAX;
|
|
||||||
|
|
||||||
open.push(State::new(map.clone()));
|
open.push(State::new(map.clone()));
|
||||||
while let Some(state) = open.pop() {
|
while let Some(state) = open.pop() {
|
||||||
let summary = StateSummary::from(&state);
|
let summary = StateSummary::from(&state);
|
||||||
if visited.contains(&summary) || best_found_for_all <= state.steps_taken {
|
if visited.contains(&summary) {
|
||||||
// there could come no better solution
|
// there could come no better solution
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if summary.1 == all_keys {
|
if summary.1 == all_keys {
|
||||||
if state.steps_taken < best_found_for_all {
|
return state.steps_taken;
|
||||||
best_found_for_all = state.steps_taken;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
state
|
state
|
||||||
@@ -56,7 +53,7 @@ fn task(map: Map) -> usize {
|
|||||||
visited.insert(summary);
|
visited.insert(summary);
|
||||||
}
|
}
|
||||||
|
|
||||||
best_found_for_all
|
panic!("if we reach this point no path can be found");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Hash, Debug)]
|
#[derive(Eq, PartialEq, Hash, Debug)]
|
||||||
|
|||||||
Reference in New Issue
Block a user