Skip to content

Commit 045e7fa

Browse files
committed
.
1 parent 5ea5ed1 commit 045e7fa

File tree

1 file changed

+65
-4
lines changed

1 file changed

+65
-4
lines changed

packages/solver-r/src/exit_cost_grid.rs

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use std::u32;
2-
3-
use crate::grid::{iter_neighbour4, Color, Grid};
1+
use crate::grid::{iter_neighbour4, Color, Grid, Point};
42

53
pub fn get_exit_cost_grid(color_grid: &Grid<Color>) -> Grid<u32> {
64
let mut exit_cost_grid = Grid::<u32>::create(color_grid.width, color_grid.height);
@@ -19,7 +17,12 @@ pub fn get_exit_cost_grid(color_grid: &Grid<Color>) -> Grid<u32> {
1917
})
2018
.min()
2119
.unwrap();
22-
let self_cost = (10 as u32).pow(color_grid.get(&p) as u32);
20+
21+
let self_cost = match color_grid.get(&p) {
22+
Color::Empty => 0,
23+
c => (256 as u32).pow((c as u32) - 1),
24+
};
25+
println!("color: {:?} cost:{}", color_grid.get(&p), self_cost);
2326

2427
let cost = neighbour_cost_min + self_cost;
2528

@@ -36,3 +39,61 @@ pub fn get_exit_cost_grid(color_grid: &Grid<Color>) -> Grid<u32> {
3639

3740
exit_cost_grid
3841
}
42+
43+
#[test]
44+
fn it_should_compute_exist_cost_grid_1() {
45+
let color_grid = Grid::<Color>::from(
46+
r#"
47+
_._
48+
"#,
49+
);
50+
let exit_grid = get_exit_cost_grid(&color_grid);
51+
52+
assert_eq!(
53+
exit_grid.to_string(),
54+
r#"
55+
010
56+
"#
57+
.trim(),
58+
)
59+
}
60+
61+
#[test]
62+
fn it_should_compute_exist_cost_grid_2() {
63+
let color_grid = Grid::<Color>::from(
64+
r#"
65+
_ .... _
66+
. ......
67+
. ... .
68+
....
69+
"#,
70+
);
71+
let exit_grid = get_exit_cost_grid(&color_grid);
72+
73+
assert_eq!(
74+
exit_grid.to_string(),
75+
r#"
76+
0011110000000
77+
0111111111110
78+
0111111110010
79+
0011110000000
80+
"#
81+
.trim(),
82+
)
83+
}
84+
85+
#[test]
86+
fn it_should_compute_exist_cost_grid_3() {
87+
let color_grid = Grid::<Color>::from(
88+
r#"
89+
#####
90+
#####
91+
## ##
92+
#####
93+
#####
94+
"#,
95+
);
96+
let exit_grid = get_exit_cost_grid(&color_grid);
97+
98+
assert_eq!(exit_grid.get(&Point { x: 2, y: 2 }), 256 * 256 * 256 * 2)
99+
}

0 commit comments

Comments
 (0)