Skip to content

Commit 32cc8b1

Browse files
committed
.
1 parent 60e3643 commit 32cc8b1

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

cargo/snk-solver/src/snake_path.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ impl PartialOrd for Node {
5555
}
5656
}
5757

58+
// move a snake from a position to a point (that it reaches with its head)
59+
//
60+
// it should be the same as path finding from cell to cell
61+
// with the caveat that is might block it-self in the first N step (for a snake of size N)
5862
pub fn get_snake_path(
5963
grid: &Grid<Color>,
6064
from: &Snake4,
@@ -80,10 +84,7 @@ pub fn get_snake_path(
8084

8185
while let Some(node) = open_list.pop() {
8286
loop_count += 1;
83-
84-
if loop_count > 20_000 {
85-
panic!("loop_count out of control")
86-
}
87+
debug_assert!(loop_count < 20_000, "invariant: loop out of control");
8788

8889
let node_cost = node.cost;
8990

cargo/snk-solver/src/solver.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ pub fn solve(color_grid: &Grid<Color>, snake: &Snake4) -> Vec<Direction> {
137137
})
138138
.collect();
139139
}
140+
141+
debug_assert!(
142+
iter_rectangle_fill(color_grid.width, color_grid.height)
143+
.filter(|p| {
144+
let color = color_grid.get_color(*p);
145+
color.is_empty() || color >= color
146+
})
147+
.next()
148+
.is_none(),
149+
"invariant: should have cleared the color"
150+
)
140151
}
141152

142153
path

0 commit comments

Comments
 (0)