1
+ use std:: fmt;
1
2
use std:: io:: stdin;
2
3
use std:: ops:: Sub ;
3
4
@@ -8,7 +9,7 @@ pub struct Elevation(u8);
8
9
9
10
impl From < char > for Elevation {
10
11
fn from ( input : char ) -> Self {
11
- Elevation ( input as u8 - b'a' )
12
+ Self ( input as u8 - b'a' )
12
13
}
13
14
}
14
15
@@ -28,23 +29,22 @@ impl Sub for Elevation {
28
29
}
29
30
}
30
31
31
- #[ derive( Clone , Copy ) ]
32
- pub struct Coord ( usize , usize ) ;
33
-
34
- impl Coord {
35
- pub fn x ( self ) -> usize {
36
- self . 0
37
- }
32
+ #[ derive( Clone , Copy , Debug , PartialEq ) ]
33
+ pub struct Coord {
34
+ pub x : usize ,
35
+ pub y : usize ,
36
+ }
38
37
39
- pub fn y ( self ) -> usize {
40
- self . 1
38
+ impl fmt:: Display for Coord {
39
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
40
+ write ! ( f, "({}, {})" , self . x, self . y)
41
41
}
42
42
}
43
43
44
44
pub struct Map {
45
45
field : ( Vec < Elevation > , usize , usize ) ,
46
46
pub start : Coord ,
47
- pub end : Coord ,
47
+ pub finish : Coord ,
48
48
}
49
49
50
50
impl Map {
@@ -74,12 +74,12 @@ impl Map {
74
74
75
75
'S' => {
76
76
field. push ( 'a' . into ( ) ) ;
77
- start = Some ( Coord ( x, y) ) ;
77
+ start = Some ( Coord { x, y } ) ;
78
78
}
79
79
80
80
'E' => {
81
81
field. push ( 'z' . into ( ) ) ;
82
- end = Some ( Coord ( x, y) ) ;
82
+ end = Some ( Coord { x, y } ) ;
83
83
}
84
84
85
85
_ => return Err ( Error :: ParseError ) ,
@@ -90,22 +90,22 @@ impl Map {
90
90
line. clear ( ) ;
91
91
}
92
92
93
- Ok ( Map {
93
+ Ok ( Self {
94
94
field : ( field, width. ok_or ( Error :: ParseError ) ?, y) ,
95
95
start : start. ok_or ( Error :: ParseError ) ?,
96
- end : end. ok_or ( Error :: ParseError ) ?,
96
+ finish : end. ok_or ( Error :: ParseError ) ?,
97
97
} )
98
98
}
99
99
100
100
pub fn size ( & self ) -> Coord {
101
- let ( _, width , height ) = self . field ;
102
- Coord ( width , height )
101
+ let ( _, x , y ) = self . field ;
102
+ Coord { x , y }
103
103
}
104
104
105
105
pub fn elevation ( & self , at : Coord ) -> Elevation {
106
106
let ( field, width, height) = & self . field ;
107
- assert ! ( at. x( ) < * width && at. y( ) < * height) ;
107
+ assert ! ( at. x < * width && at. y < * height) ;
108
108
109
- field[ at. x ( ) + ( at. y ( ) * width) ]
109
+ field[ at. x + ( at. y * width) ]
110
110
}
111
111
}
0 commit comments