File tree 1 file changed +20
-0
lines changed
1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change
1
+ //! # Point of Incidence
2
+ //!
3
+ //! We store each row of a grid as a binary number. For example `#.##..##.` becomes `101100110`.
4
+ //! Then to count smudges we bitwise XOR the respective rows together and count one bits
5
+ //! using the [`count_ones`] function.
6
+ //!
7
+ //! For example:
8
+ //! ```none
9
+ //! ..##..### 001100111 ^ 000100111 = 00100000 => 1
10
+ //! v#####.##.v => 111110110 ^ 111110110 = 00000000 => 0
11
+ //! ^#####.##.^
12
+ //! ...#..###
13
+ //! ````
14
+ //!
15
+ //! To handle columns we transpose the grid then convert into integers the same way. For part one
16
+ //! we look for a reflection axis with 0 smudges and for part two 1 smudge, allowing the same
17
+ //! code to be reused.
18
+ //!
19
+ //! [`count_ones`]: u32::count_ones
1
20
use crate :: util:: grid:: * ;
2
21
use crate :: util:: point:: * ;
3
22
@@ -65,6 +84,7 @@ fn reflect_axis(axis: &[u32], target: u32) -> Option<usize> {
65
84
for i in 1 ..size {
66
85
let mut smudges = 0 ;
67
86
87
+ // Only consider rows/columns within the boundary of the grid.
68
88
for j in 0 ..i. min ( size - i) {
69
89
smudges += ( axis[ i - j - 1 ] ^ axis[ i + j] ) . count_ones ( ) ;
70
90
}
You can’t perform that action at this time.
0 commit comments