From d02c5cb02354fbfff230ffd80484d35d1c12c05b Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Fri, 12 Jun 2020 14:08:10 +0200 Subject: [PATCH] Fix node skipping by computing the accumulated proximity --- src/best_proximity.rs | 12 ++++++++---- src/lib.rs | 5 ++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/best_proximity.rs b/src/best_proximity.rs index 1809ac770..bb611044e 100644 --- a/src/best_proximity.rs +++ b/src/best_proximity.rs @@ -35,6 +35,8 @@ enum Node { layer: usize, // The position where this node is located. position: u32, + // The total accumulated proximity until this node, used for skipping nodes. + acc_proximity: u32, }, } @@ -45,17 +47,19 @@ impl Node { fn successors(&self, positions: &[Vec], best_proximity: u32) -> Vec<(Node, u32)> { match self { Node::Uninit => { - positions[0].iter().map(|p| (Node::Init { layer: 0, position: *p }, 0)).collect() + positions[0].iter().map(|p| { + (Node::Init { layer: 0, position: *p, acc_proximity: 0 }, 0) + }).collect() }, // We reached the highest layer n @ Node::Init { .. } if n.is_complete(positions) => vec![], - Node::Init { layer, position } => { + Node::Init { layer, position, acc_proximity } => { let layer = layer + 1; positions[layer].iter().filter_map(|p| { let proximity = positions_proximity(*position, *p); - let node = Node::Init { layer, position: *p }; + let node = Node::Init { layer, position: *p, acc_proximity: acc_proximity + proximity }; // We do not produce the nodes we have already seen in previous iterations loops. - if node.is_complete(positions) && proximity < best_proximity { + if node.is_complete(positions) && acc_proximity + proximity < best_proximity { None } else { Some((node, proximity)) diff --git a/src/lib.rs b/src/lib.rs index c2888334a..5e2c6192a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -121,7 +121,10 @@ impl Index { for (proximity, mut positions) in BestProximity::new(positions) { // TODO we must ignore positions paths that gives nothing - if words.len() > 1 && proximity == 0 { continue } + if (proximity as usize) < words.len() - 1 { + eprintln!("Skipping too short proximities of {}.", proximity); + continue + } positions.sort_unstable();