@@ -6,7 +6,9 @@ use std::{
6
6
ops:: { Index , Range } ,
7
7
} ;
8
8
9
- pub const DIRS : [ ( i64 , i64 ) ; 4 ] = [ ( 1 , 0 ) , ( -1 , 0 ) , ( 0 , 1 ) , ( 0 , -1 ) ] ;
9
+ // Right, Down, Left, Up
10
+ pub const DIRS : [ ( i64 , i64 ) ; 4 ] = [ ( 1 , 0 ) , ( 0 , 1 ) , ( -1 , 0 ) , ( 0 , -1 ) ] ;
11
+
10
12
pub const CLOCKWISE : [ ( i64 , i64 ) ; 8 ] = [
11
13
( 1 , 0 ) ,
12
14
( 1 , 1 ) ,
@@ -18,16 +20,26 @@ pub const CLOCKWISE: [(i64, i64); 8] = [
18
20
( 1 , -1 ) ,
19
21
] ;
20
22
21
- pub trait ReadToGrid {
22
- fn read_to_grid ( & self ) -> Grid < char > ;
23
+ pub fn read_to_grid ( filename : & str ) -> Result < Grid < char > , std :: io :: Error > {
24
+ Ok ( fs :: read_to_string ( filename ) ? . to_grid ( ) )
23
25
}
24
26
25
- pub fn read_to_grid ( filename : & str ) -> Result < Grid < char > , std:: io:: Error > {
26
- let g = fs:: read_to_string ( filename) ?
27
- . lines ( )
28
- . map ( |l| l. chars ( ) . collect ( ) )
29
- . collect ( ) ;
30
- Ok ( Grid { grid : g } )
27
+ pub trait ToGrid {
28
+ fn to_grid ( & self ) -> Grid < char > ;
29
+ }
30
+
31
+ impl ToGrid for & str {
32
+ fn to_grid ( & self ) -> Grid < char > {
33
+ Grid {
34
+ grid : self . lines ( ) . map ( |l| l. chars ( ) . collect ( ) ) . collect ( ) ,
35
+ }
36
+ }
37
+ }
38
+
39
+ impl ToGrid for String {
40
+ fn to_grid ( & self ) -> Grid < char > {
41
+ self . as_str ( ) . to_grid ( )
42
+ }
31
43
}
32
44
33
45
#[ derive( Clone , PartialEq , Eq , Debug , Hash ) ]
@@ -66,16 +78,6 @@ impl<T: Copy> Grid<T> {
66
78
self . grid [ y as usize ] [ x as usize ] = c;
67
79
}
68
80
69
- #[ inline]
70
- pub fn row_range ( & self ) -> Range < i64 > {
71
- 0 ..self . height ( )
72
- }
73
-
74
- #[ inline]
75
- pub fn col_range ( & self ) -> Range < i64 > {
76
- 0 ..self . width ( )
77
- }
78
-
79
81
pub fn iter ( & self ) -> GridIterator < T > {
80
82
GridIterator {
81
83
grid : self ,
@@ -110,14 +112,6 @@ impl<'a, T: Copy> Iterator for GridIterator<'a, T> {
110
112
}
111
113
}
112
114
113
- impl < T : Copy > Index < ( i64 , i64 ) > for Grid < T > {
114
- type Output = T ;
115
-
116
- fn index ( & self , ( x, y) : ( i64 , i64 ) ) -> & Self :: Output {
117
- & self . grid [ y as usize ] [ x as usize ]
118
- }
119
- }
120
-
121
115
impl < T : Copy + Display > Display for Grid < T > {
122
116
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result < ( ) , core:: fmt:: Error > {
123
117
for row in self . grid . iter ( ) {
0 commit comments