Skip to content

Commit c135277

Browse files
committed
.
1 parent 34d5617 commit c135277

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

packages/solver-r/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl From<IGrid> for Grid {
7171
pub fn iget_free_cell(grid: &IGrid) -> js_sys::Uint8Array {
7272
let g = Grid::from(grid.clone());
7373

74-
let out = get_free_cell(&g, Cell::Color1);
74+
let (_, out) = get_free_cell(&g, Cell::Color1);
7575

7676
let o: Vec<u8> = out.iter().flat_map(|p| [p.x as u8, p.y as u8]).collect();
7777

packages/solver-r/src/solver.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use std::collections::HashSet;
22

33
use crate::grid::{Cell, Grid, Point};
44

5-
pub fn get_free_cell(grid: &Grid, walkable: Cell) -> HashSet<Point> {
6-
let mut free: HashSet<Point> = HashSet::new();
5+
pub fn get_free_cell(grid: &Grid, walkable: Cell) -> (HashSet<Point>, HashSet<Point>) {
6+
let mut free_cells: HashSet<Point> = HashSet::new();
7+
let mut one_way_cells: HashSet<Point> = HashSet::new();
78
let mut open_list: HashSet<Point> = HashSet::new();
89

910
for x in 0..(grid.width as i8) {
@@ -43,7 +44,7 @@ pub fn get_free_cell(grid: &Grid, walkable: Cell) -> HashSet<Point> {
4344
};
4445

4546
if !visited.contains(&neighbour)
46-
&& (free.contains(&neighbour) || !grid.is_inside(&neighbour))
47+
&& (free_cells.contains(&neighbour) || !grid.is_inside(&neighbour))
4748
{
4849
visited.insert(neighbour);
4950
exit_count += 1;
@@ -67,7 +68,7 @@ pub fn get_free_cell(grid: &Grid, walkable: Cell) -> HashSet<Point> {
6768

6869
if !visited.contains(&neighbour)
6970
&& !visited.contains(&corner)
70-
&& (free.contains(&corner) || !grid.is_inside(&corner))
71+
&& (free_cells.contains(&corner) || !grid.is_inside(&corner))
7172
{
7273
visited.insert(neighbour);
7374
visited.insert(corner);
@@ -81,25 +82,29 @@ pub fn get_free_cell(grid: &Grid, walkable: Cell) -> HashSet<Point> {
8182
};
8283

8384
if has_enough_free_exits {
84-
free.insert(p);
85+
free_cells.insert(p);
8586

8687
for dir in directions {
8788
let neighbour = Point {
8889
x: p.x + dir.x,
8990
y: p.y + dir.y,
9091
};
9192

92-
if !free.contains(&neighbour)
93+
if !free_cells.contains(&neighbour)
9394
&& grid.is_inside(&neighbour)
9495
&& grid.get_cell(&neighbour) <= walkable
9596
{
9697
open_list.insert(neighbour);
9798
}
9899
}
100+
} else {
101+
one_way_cells.insert(p);
99102
}
100103
}
101104

102-
free
105+
one_way_cells.retain(|p| !free_cells.contains(&p));
106+
107+
(free_cells, one_way_cells)
103108
}
104109

105110
#[test]
@@ -108,7 +113,7 @@ fn it_should_collect_free_cell() {
108113

109114
grid.set_cell(&Point { x: 1, y: 1 }, Cell::Color2);
110115

111-
let free_cells = get_free_cell(&grid, Cell::Color1);
116+
let (free_cells, _) = get_free_cell(&grid, Cell::Color1);
112117

113118
assert_eq!(
114119
free_cells,

0 commit comments

Comments
 (0)