File tree Expand file tree Collapse file tree 1 file changed +11
-15
lines changed Expand file tree Collapse file tree 1 file changed +11
-15
lines changed Original file line number Diff line number Diff line change @@ -81,26 +81,22 @@ impl Day20 {
8181
8282 fn cheat_count ( & self , x_offset : isize , y_offset : isize ) -> u32 {
8383 let cheat_length = ( x_offset. unsigned_abs ( ) + y_offset. unsigned_abs ( ) ) as u16 ;
84+ let threshold = 101 + cheat_length;
8485 if cheat_length == 0 {
8586 return 0 ;
8687 }
8788
88- let mut cheats = 0 ;
89+ let start_index = self . cols * 20 + 20 ;
90+ let end_index = self . distances . len ( ) - start_index;
8991 let offset = y_offset * ( self . cols as isize ) + x_offset;
90- for ( index, target) in ( self . cols * 20 ..self . distances . len ( ) - ( self . cols * 20 ) )
91- . zip ( ( self . cols * 20 ) . wrapping_add_signed ( offset) ..)
92- {
93- let this_distance = self . distances [ index] ;
94- let target_distance = self . distances [ target] ;
95- cheats += u16:: from (
96- target_distance
97- . wrapping_add ( 1 )
98- . saturating_sub ( this_distance)
99- . saturating_sub ( cheat_length)
100- >= 101 ,
101- ) ;
102- }
103- cheats as u32
92+
93+ self . distances [ start_index..end_index]
94+ . iter ( )
95+ . zip ( self . distances [ start_index. wrapping_add_signed ( offset) ..] . iter ( ) )
96+ . map ( |( & current, & target) | {
97+ u16:: from ( target. wrapping_add ( 1 ) . saturating_sub ( current) >= threshold)
98+ } )
99+ . sum :: < u16 > ( ) as u32
104100 }
105101}
106102
You can’t perform that action at this time.
0 commit comments