I was at a carnival where at each location they mark your program with a special hole punch. The hole punch is a grid of 3×3 spaces. In each space, there’s either a pin that punctures your paper or there isn’t. This got me to wondering how many different patterns you could make with this tool. My first thought was: 2^9 = 512, but all 9 spaces being pinless isn’t really a punch, so really: 511.
Then the complexity hit me. Especially since the workers aren’t all that careful when they punch your paper, these would all look idential:
x.. .x. ... etc.
.x. x.. .x.
... ... ..x
Question: How could a test be written to account for rotation and shifting?
Diligence and thoughts so far:
- Binary feels like an obvious part of this equation
- When a unique pattern is found, store it in memory so future patterns can be tested against it
- There are 4 rotation possibilities.
Edit: what I mean by “rotations” is that you can take any shape and turn it 90 degrees. Consider the pattern that is a dot in the upper left corner. You can turn/rotate it 90 degrees and get the dot in the upper right corner. Do this again and it’s in the lower right. Again and it’s in the lower left. Using the pure 2^9 calculation, these are 4 different combinations. For this problem however, these are exactly the kind of duplicates I’m trying to weed out. - For each rotation, there are 25 ways to make 3×3 grids overlap:
Overlaps:
/ = the spaces in the new one to test
\ = the spaces in a verified unique one
1 2 25
/ / / . . . . . / / / . . . . . . . . . .
/ / / . . . . . / / / . . . . . . . . . .
/ / X \ \ . . . / X X \ . . . . \ \ \ . .
. . \ \ \ . . . . \ \ \ . . . . \ \ \ . .
. . \ \ \ . . . . \ \ \ . . . . \ \ X / /
. . . . . . . . . . . . . . . . . . / / /
. . . . . . . . . . . . . . . . . . / / /
- An overlap doesn’t need to be tested if either pattern contains a pin that isn’t in the overlap area. Bitwise AND could help here.
- If you make each position for each of the 2 patterns into strings, you can just check for equality
- Can these previous two ideas be combined to increase efficiency?
We need to only consider patterns that have punches in the first row and column. If the first row is empty, the pattern can be shifted up. If the first column is empty, the pattern can be shifted left. In either case, we can derive a similar pattern that we do consider.
For these patterns, we need to check if the rotated versions are identical. We do this by applying up to three 90 degree rotations, possibly shifting left to remove leading empty columns (the first row is never empty) and finding the pattern with the lowest numeric value.
We can then add this value to a hash set, which will only keep unique values.
The empty pattern is not included because all its rows are empty.
To implement this, we encode patterns as successive bits:
The operations we will need are mostly very simple:
The trickiest part is the rotation, which is really just rearranging all the bits:
In C#:
This function returns 116. The time taken on my machine was 0.023ms.
EDIT: You can get an additional 7x improvement by using 4 observations:
So, if we apply these observations and unroll the inner do loop, we get the following:
This runs in about 3μs on the same machine.