@@ -37,33 +37,33 @@ impl Day09 {
3737 let batches = marbles / 23 ;
3838 for base in ( 0 ..23 * batches) . step_by ( 23 ) {
3939 // Equivalent to the following operations, which naively add 23 marbles while keeping
40- // the current marble at the front of dequeue:
41- // 22x [push_back(pop_front ), push_back(pop_front ), push_front (i)]
42- // 7x [push_front(pop_back )]
43- // [pop_front ]
40+ // the current marble at the back of dequeue:
41+ // 22x [push_front(pop_back ), push_front(pop_back ), push_back (i)]
42+ // 7x [push_back(pop_front )]
43+ // [pop_back ]
4444 // By eliminating redundant pushes and pops the total number of operations per batch is
4545 // decreased from 125 to 67.
46- let front = circle. pop_front ( ) . unwrap ( ) ;
47- circle. push_back ( front) ;
46+ let front = circle. pop_back ( ) . unwrap ( ) ;
47+ circle. push_front ( front) ;
4848
4949 for i in 1 ..=18 {
50- let front = circle. pop_front ( ) . unwrap ( ) ;
51- circle. push_back ( front) ;
52- circle. push_back ( base + i) ;
50+ let front = circle. pop_back ( ) . unwrap ( ) ;
51+ circle. push_front ( front) ;
52+ circle. push_front ( base + i) ;
5353 }
5454
55- let f1 = circle. pop_front ( ) . unwrap ( ) ;
56- let f2 = circle. pop_front ( ) . unwrap ( ) ;
57- let f3 = circle. pop_front ( ) . unwrap ( ) ;
58- let f4 = circle. pop_front ( ) . unwrap ( ) ;
55+ let f1 = circle. pop_back ( ) . unwrap ( ) ;
56+ let f2 = circle. pop_back ( ) . unwrap ( ) ;
57+ let f3 = circle. pop_back ( ) . unwrap ( ) ;
58+ let f4 = circle. pop_back ( ) . unwrap ( ) ;
5959
60- circle. push_front ( base + 22 ) ;
61- circle. push_front ( f4) ;
62- circle. push_front ( base + 21 ) ;
63- circle. push_front ( f3) ;
64- circle. push_front ( base + 20 ) ;
65- circle. push_front ( f2) ;
66- circle. push_front ( base + 19 ) ;
60+ circle. push_back ( base + 22 ) ;
61+ circle. push_back ( f4) ;
62+ circle. push_back ( base + 21 ) ;
63+ circle. push_back ( f3) ;
64+ circle. push_back ( base + 20 ) ;
65+ circle. push_back ( f2) ;
66+ circle. push_back ( base + 19 ) ;
6767
6868 scores[ ( ( base + 23 ) % players) as usize ] += ( base as u64 + 23 ) + ( f1 as u64 ) ;
6969 }
0 commit comments