@@ -2,27 +2,21 @@ use snk_grid::{
22 color:: Color ,
33 direction:: { Direction , add_direction, iter_directions} ,
44 grid:: { Grid , iter_rectangle_hull} ,
5+ grid_ascii:: grid_to_ascii_transformed,
56 point:: Point ,
67} ;
78use std:: collections:: HashSet ;
89
10+ use crate :: cost:: Cost ;
11+
912#[ derive( Copy , Clone ) ]
1013pub struct ExitDirection {
11- pub cost : u32 ,
14+ pub cost : Cost ,
1215 pub exit_direction : Direction ,
1316}
1417impl ExitDirection {
1518 pub fn is_outside ( & self ) -> bool {
16- self . cost == 0
17- }
18- }
19- impl ToString for ExitDirection {
20- fn to_string ( & self ) -> String {
21- if self . cost == 0 {
22- "o" . to_string ( )
23- } else {
24- self . exit_direction . to_string ( )
25- }
19+ self . cost . is_free ( )
2620 }
2721}
2822
@@ -33,7 +27,7 @@ pub fn create_path_to_outside(grid: &Grid<Color>) -> Grid<ExitDirection> {
3327 grid. width ,
3428 grid. height ,
3529 ExitDirection {
36- cost : u32 :: MAX ,
30+ cost : Cost :: max ( ) ,
3731 exit_direction : Direction :: UP ,
3832 } ,
3933 ) ;
@@ -53,16 +47,16 @@ pub fn create_path_to_outside(grid: &Grid<Color>) -> Grid<ExitDirection> {
5347 } {
5448 changed. remove ( & p) ;
5549
56- let cost = if path_to_outside. is_inside ( p) {
50+ let cost: Cost = if path_to_outside. is_inside ( p) {
5751 path_to_outside. get ( p) . cost
5852 } else {
59- 0
53+ Cost :: zero ( )
6054 } ;
6155
6256 for dir in iter_directions ( ) {
6357 let p = add_direction ( p, dir) ;
6458 if path_to_outside. is_inside ( p) {
65- let new_cost = cost + grid. get ( p) . cost ( ) ;
59+ let new_cost = cost + grid. get ( p) . into ( ) ;
6660
6761 let c = path_to_outside. get_mut ( p) ;
6862
@@ -79,25 +73,30 @@ pub fn create_path_to_outside(grid: &Grid<Color>) -> Grid<ExitDirection> {
7973}
8074
8175#[ test]
82- #[ ignore]
8376fn it_should_compute_the_cost_to_outside ( ) {
8477 let grid = Grid :: < _ > :: from (
8578 r#"
8679_...._
87- _. . ._
88- _... ._
89- _.... _
80+ _. ._
81+ _.. ._
82+ _. _
9083"# ,
9184 ) ;
9285 let pto = create_path_to_outside ( & grid) ;
9386
9487 assert_eq ! (
95- pto. to_string( ) ,
88+ grid_to_ascii_transformed( & pto, |c| {
89+ if c. cost. is_free( ) {
90+ c. cost. 0 . to_string( )
91+ } else {
92+ "#" . to_string( )
93+ }
94+ } ) ,
9695 r#"
97- o↑↑↑→o
98- o←←→→o
99- o←↑↓→o
100- o←↓↓→o
96+ 1####1
97+ 1#43#1
98+ 1##2#1
99+ 1#1111
101100"#
102101 . trim( ) ,
103102 ) ;
0 commit comments