1+ use crate :: pos:: Pos ;
12use std:: fmt;
23use std:: fmt:: Write ;
34
45pub fn part1 ( mine : & Mine ) -> String {
5- format ! ( "{:?}" , mine. clone( ) . first_crash( ) )
6+ mine. clone ( ) . first_crash ( ) . to_string ( )
67}
78
89pub fn part2 ( mine : & Mine ) -> String {
9- format ! ( "{:?}" , mine. clone( ) . last_cart( ) )
10+ mine. clone ( ) . last_cart ( ) . to_string ( )
1011}
1112
1213#[ derive( Clone ) ]
@@ -69,22 +70,9 @@ enum Turn {
6970 Right ,
7071}
7172
72- #[ derive( PartialEq ) ]
73- struct Position {
74- x : usize ,
75- y : usize ,
76- }
77-
78- impl fmt:: Debug for Position {
79- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
80- write ! ( f, "{},{}" , self . x, self . y)
81- }
82- }
83-
8473#[ derive( Debug , Clone ) ]
8574struct Cart {
86- x : usize ,
87- y : usize ,
75+ pos : Pos ,
8876 facing : Direction ,
8977 next_turn : Turn ,
9078 destroyed : bool ,
@@ -112,13 +100,13 @@ impl Cart {
112100
113101 fn move_turn ( & mut self , map : & [ Vec < Cell > ] ) {
114102 match self . facing {
115- Direction :: North => self . y -= 1 ,
116- Direction :: East => self . x += 1 ,
117- Direction :: South => self . y += 1 ,
118- Direction :: West => self . x -= 1 ,
103+ Direction :: North => self . pos . y -= 1 ,
104+ Direction :: East => self . pos . x += 1 ,
105+ Direction :: South => self . pos . y += 1 ,
106+ Direction :: West => self . pos . x -= 1 ,
119107 }
120108
121- match map[ self . y ] [ self . x ] {
109+ match map[ self . pos . y as usize ] [ self . pos . x as usize ] {
122110 Cell :: Corner1 => match self . facing {
123111 Direction :: North => self . facing = Direction :: East ,
124112 Direction :: East => self . facing = Direction :: North ,
@@ -148,7 +136,10 @@ impl fmt::Debug for Mine {
148136 let mut i = 0 ;
149137 for ( y, row) in self . map . iter ( ) . enumerate ( ) {
150138 for ( x, cell) in row. iter ( ) . enumerate ( ) {
151- if i < self . carts . len ( ) && self . carts [ i] . y == y && self . carts [ i] . x == x {
139+ if i < self . carts . len ( )
140+ && self . carts [ i] . pos . y == y as i32
141+ && self . carts [ i] . pos . x == x as i32
142+ {
152143 f. write_char ( self . carts [ i] . facing . to_char ( ) ) . unwrap ( ) ;
153144 i += 1 ;
154145 } else {
@@ -162,39 +153,38 @@ impl fmt::Debug for Mine {
162153}
163154
164155impl Mine {
165- fn first_crash ( & mut self ) -> Position {
156+ fn first_crash ( & mut self ) -> Pos {
166157 loop {
167158 for cart_id in 0 ..self . carts . len ( ) {
168159 self . carts [ cart_id] . move_turn ( & self . map ) ;
169160
170161 // has it crashed?
171162 if self . detect_crash ( cart_id) {
172- return Position {
173- x : self . carts [ cart_id] . x ,
174- y : self . carts [ cart_id] . y ,
163+ return Pos {
164+ x : self . carts [ cart_id] . pos . x ,
165+ y : self . carts [ cart_id] . pos . y ,
175166 } ;
176167 }
177168 }
178- self . carts . sort_by_key ( |c| ( c. y , c. x ) ) ;
169+ self . carts . sort_by_key ( |c| ( c. pos . y , c. pos . x ) ) ;
179170 }
180171 }
181172
182173 fn detect_crash ( & mut self , this_id : usize ) -> bool {
183- let this_x = self . carts [ this_id] . x ;
184- let this_y = self . carts [ this_id] . y ;
174+ let this_pos = self . carts [ this_id] . pos ;
185175 for ( other_id, other) in self . carts . iter_mut ( ) . enumerate ( ) {
186176 if other_id == this_id {
187177 continue ;
188178 }
189- if this_x == other. x && this_y == other . y {
179+ if this_pos == other. pos {
190180 other. destroyed = true ;
191181 return true ;
192182 }
193183 }
194184 false
195185 }
196186
197- fn last_cart ( & mut self ) -> Position {
187+ fn last_cart ( & mut self ) -> Pos {
198188 loop {
199189 for cart_id in 0 ..self . carts . len ( ) {
200190 self . carts [ cart_id] . move_turn ( & self . map ) ;
@@ -205,12 +195,9 @@ impl Mine {
205195 }
206196 self . carts . retain ( |cart| !cart. destroyed ) ;
207197 if self . carts . len ( ) == 1 {
208- return Position {
209- x : self . carts [ 0 ] . x ,
210- y : self . carts [ 0 ] . y ,
211- } ;
198+ return self . carts [ 0 ] . pos ;
212199 }
213- self . carts . sort_by_key ( |c| ( c. y , c. x ) ) ;
200+ self . carts . sort_by_key ( |c| ( c. pos . y , c. pos . x ) ) ;
214201 }
215202 }
216203}
@@ -236,8 +223,10 @@ pub fn parse_input(input: &str) -> Mine {
236223 mine. map [ y] [ x] = Cell :: EW ;
237224 }
238225 mine. carts . push ( Cart {
239- x,
240- y,
226+ pos : Pos {
227+ x : x as i32 ,
228+ y : y as i32 ,
229+ } ,
241230 facing : Direction :: from_char ( c) . unwrap ( ) ,
242231 next_turn : Turn :: Left ,
243232 destroyed : false ,
@@ -275,12 +264,12 @@ mod tests {
275264 #[ test]
276265 fn test_first_crash ( ) {
277266 let mut mine = parse_input ( test_input ( ) ) ;
278- assert_eq ! ( Position { x: 7 , y: 3 } , mine. first_crash( ) ) ;
267+ assert_eq ! ( Pos { x: 7 , y: 3 } , mine. first_crash( ) ) ;
279268 }
280269
281270 #[ test]
282271 fn test_last_cart ( ) {
283272 let mut mine = parse_input ( test_input2 ( ) ) ;
284- assert_eq ! ( Position { x: 6 , y: 4 } , mine. last_cart( ) ) ;
273+ assert_eq ! ( Pos { x: 6 , y: 4 } , mine. last_cart( ) ) ;
285274 }
286275}
0 commit comments