File tree Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,11 @@ use crate::vector::Vector;
77
88pub type Pos = Vector < i64 , 2 > ;
99
10+ const UP : Vector < i64 , 2 > = Vector :: new ( [ 0 , -1 ] ) ;
11+ const DOWN : Vector < i64 , 2 > = Vector :: new ( [ 0 , 1 ] ) ;
12+ const LEFT : Vector < i64 , 2 > = Vector :: new ( [ -1 , 0 ] ) ;
13+ const RIGHT : Vector < i64 , 2 > = Vector :: new ( [ 1 , 0 ] ) ;
14+
1015#[ derive( Debug , Clone ) ]
1116pub struct Grid {
1217 data : BTreeMap < Pos , char > ,
@@ -27,6 +32,19 @@ impl Grid {
2732 end : self . end ,
2833 }
2934 }
35+
36+ pub fn adjacent ( & self , pos : Pos ) -> Vec < Pos > {
37+ [ UP , DOWN , LEFT , RIGHT ] . into_iter ( )
38+ . map ( |d| pos + d)
39+ . filter ( |& p| self . valid ( p) )
40+ . collect ( )
41+ }
42+
43+ pub fn valid ( & self , pos : Pos ) -> bool {
44+ let ( x_range, y_range) = self . range ( ) ;
45+
46+ x_range. contains ( & pos[ 0 ] ) && y_range. contains ( & pos[ 1 ] )
47+ }
3048}
3149
3250impl Display for Grid {
You can’t perform that action at this time.
0 commit comments