@@ -17,6 +17,34 @@ pub struct Robot {
1717}
1818
1919impl Robot {
20+ fn clone_from ( robot : & Robot ) -> Robot {
21+ let current_point = robot. current_point ;
22+
23+ let bodies_diff = vec ! [
24+ Point :: new( 0 , 0 ) ,
25+ Point :: new( 1 , 1 ) ,
26+ Point :: new( 1 , 0 ) ,
27+ Point :: new( 1 , -1 ) ,
28+ ] ;
29+ let new_bodies = VecDeque :: from ( vec ! [
30+ Point :: new( -1 , 0 ) ,
31+ Point :: new( -1 , 1 ) ,
32+ Point :: new( -1 , -1 ) ,
33+ Point :: new( 0 , -1 ) ,
34+ Point :: new( 0 , 1 ) ,
35+ ] ) ;
36+
37+ let commands = robot. commands . clone ( ) ;
38+ let executed = Vec :: new ( ) ;
39+ Robot {
40+ current_point,
41+ bodies_diff,
42+ new_bodies,
43+ commands,
44+ executed,
45+ }
46+ }
47+
2048 fn initialize ( task : & Task ) -> Robot {
2149 let current_point = task. initial ;
2250
@@ -251,13 +279,15 @@ impl<'a> State<'a> {
251279 }
252280
253281 pub fn fill_next_command ( & mut self , robot_idx : usize ) {
282+ assert ! ( self . turn <= self . robots[ robot_idx] . commands. len( ) ) ;
254283 let current_point = self . robots [ robot_idx] . current_point ;
255284
256285 if self . hand_count > 0 {
257286 let robot = & mut self . robots [ robot_idx] ;
258287 if let Some ( new_hand) = robot. consume_new_hand ( ) {
259288 self . hand_count -= 1 ;
260- robot. commands . push ( Command :: NewHand ( new_hand) ) ;
289+ robot. commands . insert ( self . turn , Command :: NewHand ( new_hand) ) ;
290+ robot. commands . truncate ( self . turn + 1 ) ;
261291 return ;
262292 }
263293 }
@@ -266,11 +296,16 @@ impl<'a> State<'a> {
266296 let robot = & mut self . robots [ robot_idx] ;
267297 if let Some ( Some ( BoosterType :: Spawn ) ) = self . booster_map . get ( current_point) {
268298 self . clone_count -= 1 ;
269- robot. commands . push ( Command :: Cloning ) ;
299+ robot. commands . insert ( self . turn , Command :: Cloning ) ;
300+ robot. commands . truncate ( self . turn + 1 ) ;
270301 return ;
271302 }
272303 }
273304
305+ if self . turn < self . robots [ robot_idx] . commands . len ( ) {
306+ return ;
307+ }
308+
274309 let base_moves = self . find_shortest_path ( robot_idx, current_point) ;
275310 if let Some ( base_moves) = base_moves {
276311 for m in & base_moves {
@@ -299,10 +334,7 @@ impl<'a> State<'a> {
299334 return false ;
300335 }
301336
302- assert ! ( turn <= self . robots[ idx] . commands. len( ) ) ;
303- if turn == self . robots [ idx] . commands . len ( ) {
304- self . fill_next_command ( idx) ;
305- }
337+ self . fill_next_command ( idx) ;
306338
307339 assert ! ( turn < self . robots[ idx] . commands. len( ) ) ;
308340 let m = self . robots [ idx] . commands [ turn] . clone ( ) ;
@@ -314,8 +346,7 @@ impl<'a> State<'a> {
314346 self . robots [ idx] . bodies_diff . push ( * p) ;
315347 }
316348 Command :: Cloning => {
317- let mut new_robot = self . robots [ idx] . clone ( ) ;
318- new_robot. executed . clear ( ) ;
349+ let new_robot = Robot :: clone_from ( & self . robots [ idx] ) ;
319350 assert ! ( new_robot. commands. len( ) == self . turn + 1 ) ;
320351 self . robots . push ( new_robot) ;
321352 }
0 commit comments