little cleaning of day 18

This commit is contained in:
Johannes
2020-01-05 16:19:05 +01:00
parent 91e7a8f3fc
commit d18cfca5ed
2 changed files with 6 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
mod tasks;
fn main() {
tasks::day24::run();
tasks::day18::run();
}

View File

@@ -15,8 +15,8 @@ pub fn run() {
let t1 = task(input.clone());
println!("Task 1: best bound to get all keys is {}", t1);
let t2 = task(input.clone().split_robot());
println!("Task 2: best bound to get all keys is {}", t2);
//let t2 = task(input.clone().split_robot());
//println!("Task 2: best bound to get all keys is {}", t2);
}
fn task(map: Map) -> usize {
@@ -32,20 +32,17 @@ fn task(map: Map) -> usize {
let mut visited: HashSet<StateSummary> = HashSet::new();
let mut open: BinaryHeap<State> = BinaryHeap::new();
let mut best_found_for_all = std::usize::MAX;
open.push(State::new(map.clone()));
while let Some(state) = open.pop() {
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
continue;
}
if summary.1 == all_keys {
if state.steps_taken < best_found_for_all {
best_found_for_all = state.steps_taken;
}
return state.steps_taken;
}
state
@@ -56,7 +53,7 @@ fn task(map: Map) -> usize {
visited.insert(summary);
}
best_found_for_all
panic!("if we reach this point no path can be found");
}
#[derive(Eq, PartialEq, Hash, Debug)]