@@ -19,7 +19,7 @@ unsafe fn part1_inner(s: &str) -> u32 {
1919 let mut y = 0 ;
2020 let mut x = 0 ;
2121 for i in 0 ..s. len ( ) {
22- let c = s [ i ] ;
22+ let c = * s . get_unchecked ( i ) ;
2323 if c == b'\n' {
2424 y += 1 ;
2525 x = 0 ;
@@ -28,13 +28,16 @@ unsafe fn part1_inner(s: &str) -> u32 {
2828
2929 let layer = ( c - b'0' ) as usize ;
3030 if layer == 0 {
31- first_map[ y * MAX_SIZE + x + MAX_SIZE ] = 1 << zero_pos;
31+ * first_map. get_unchecked_mut ( y * MAX_SIZE + x + MAX_SIZE ) = 1 << zero_pos;
3232 zero_pos += 1 ;
3333 zero_pos %= 64 ;
3434 } else {
35- let len = positions[ layer - 1 ] . 0 as usize ;
36- positions[ layer - 1 ] . 1 [ len] = ( y * MAX_SIZE + x + MAX_SIZE ) as u16 ;
37- positions[ layer - 1 ] . 0 += 1 ;
35+ let len = positions. get_unchecked ( layer - 1 ) . 0 as usize ;
36+ * positions
37+ . get_unchecked_mut ( layer - 1 )
38+ . 1
39+ . get_unchecked_mut ( len) = ( y * MAX_SIZE + x + MAX_SIZE ) as u16 ;
40+ positions. get_unchecked_mut ( layer - 1 ) . 0 += 1 ;
3841 }
3942 x += 1 ;
4043 }
@@ -45,23 +48,28 @@ unsafe fn part1_inner(s: &str) -> u32 {
4548 let mut current = & mut first_map;
4649
4750 for layer in 0 ..8 {
48- let ( len, positions) = positions[ layer] ;
51+ let ( len, positions) = * positions. get_unchecked ( layer) ;
4952
50- for i in & positions[ .. len as usize ] {
53+ for i in & * positions. get_unchecked ( .. len as usize ) {
5154 let i = * i as usize ;
52- next[ i] =
53- current[ i - 1 ] | current[ i + 1 ] | current[ i + MAX_SIZE ] | current[ i - MAX_SIZE ] ;
55+ * next. get_unchecked_mut ( i) = * current. get_unchecked ( i - 1 )
56+ | * current. get_unchecked ( i + 1 )
57+ | * current. get_unchecked ( i + MAX_SIZE )
58+ | * current. get_unchecked ( i - MAX_SIZE ) ;
5459 }
5560
5661 std:: mem:: swap ( & mut current, & mut next) ;
5762 next. fill ( 0 ) ;
5863 }
5964
60- let ( len9, positions9) = positions[ 8 ] ;
65+ let ( len9, positions9) = * positions. get_unchecked ( 8 ) ;
6166 for i in & positions9[ ..len9 as usize ] {
6267 let i = * i as usize ;
63- sum += ( current[ i - 1 ] | current[ i + 1 ] | current[ i + MAX_SIZE ] | current[ i - MAX_SIZE ] )
64- . count_ones ( ) ;
68+ sum += ( * current. get_unchecked ( i - 1 )
69+ | * current. get_unchecked ( i + 1 )
70+ | * current. get_unchecked ( i + MAX_SIZE )
71+ | * current. get_unchecked ( i - MAX_SIZE ) )
72+ . count_ones ( ) ;
6573 }
6674
6775 sum
@@ -82,7 +90,7 @@ unsafe fn part2_inner(s: &str) -> u16 {
8290 let mut y = 0 ;
8391 let mut x = 0 ;
8492 for i in 0 ..s. len ( ) {
85- let c = s [ i ] ;
93+ let c = * s . get_unchecked ( i ) ;
8694 if c == b'\n' {
8795 y += 1 ;
8896 x = 0 ;
@@ -91,11 +99,14 @@ unsafe fn part2_inner(s: &str) -> u16 {
9199
92100 let layer = ( c - b'0' ) as usize ;
93101 if layer == 0 {
94- first_map[ y * MAX_SIZE + x + MAX_SIZE ] = 1 ;
102+ * first_map. get_unchecked_mut ( y * MAX_SIZE + x + MAX_SIZE ) = 1 ;
95103 } else {
96- let len = positions[ layer - 1 ] . 0 as usize ;
97- positions[ layer - 1 ] . 1 [ len] = ( y * MAX_SIZE + x + MAX_SIZE ) as u16 ;
98- positions[ layer - 1 ] . 0 += 1 ;
104+ let len = positions. get_unchecked ( layer - 1 ) . 0 as usize ;
105+ * positions
106+ . get_unchecked_mut ( layer - 1 )
107+ . 1
108+ . get_unchecked_mut ( len) = ( y * MAX_SIZE + x + MAX_SIZE ) as u16 ;
109+ positions. get_unchecked_mut ( layer - 1 ) . 0 += 1 ;
99110 }
100111 x += 1 ;
101112 }
@@ -106,22 +117,27 @@ unsafe fn part2_inner(s: &str) -> u16 {
106117 let mut current = & mut first_map;
107118
108119 for layer in 0 ..8 {
109- let ( len, positions) = positions[ layer] ;
120+ let ( len, positions) = * positions. get_unchecked ( layer) ;
110121
111- for i in & positions[ .. len as usize ] {
122+ for i in & * positions. get_unchecked ( .. len as usize ) {
112123 let i = * i as usize ;
113- next[ i] =
114- current[ i - 1 ] + current[ i + 1 ] + current[ i + MAX_SIZE ] + current[ i - MAX_SIZE ] ;
124+ * next. get_unchecked_mut ( i) = * current. get_unchecked ( i - 1 )
125+ + * current. get_unchecked ( i + 1 )
126+ + * current. get_unchecked ( i + MAX_SIZE )
127+ + * current. get_unchecked ( i - MAX_SIZE ) ;
115128 }
116129
117130 std:: mem:: swap ( & mut current, & mut next) ;
118131 next. fill ( 0 ) ;
119132 }
120133
121- let ( len9, positions9) = positions[ 8 ] ;
134+ let ( len9, positions9) = * positions. get_unchecked ( 8 ) ;
122135 for i in & positions9[ ..len9 as usize ] {
123136 let i = * i as usize ;
124- sum += current[ i - 1 ] + current[ i + 1 ] + current[ i + MAX_SIZE ] + current[ i - MAX_SIZE ] ;
137+ sum += * current. get_unchecked ( i - 1 )
138+ + * current. get_unchecked ( i + 1 )
139+ + * current. get_unchecked ( i + MAX_SIZE )
140+ + * current. get_unchecked ( i - MAX_SIZE ) ;
125141 }
126142
127143 sum
0 commit comments