Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6666179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:48:05+00:00 2026-05-26T02:48:05+00:00

I was at a carnival where at each location they mark your program with

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:48:05+00:00Added an answer on May 26, 2026 at 2:48 am

    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:

    012
    345
    678
    

    The operations we will need are mostly very simple:

    Test for an empty row:    (n & 7) == 0     // bits 0,1,2 not set
    Test for an empty column: (n & 73) == 0    // bits 0,3,6 not set
    Shift pattern up:         n -> (n >> 3)
    Shift pattern left:       n -> (n >> 1)
    

    The trickiest part is the rotation, which is really just rearranging all the bits:

    n -> ((n & 1) << 2) + ((n & 2) << 4) + ((n & 4) << 6)
       + ((n & 8) >> 2) + (n & 16) + ((n & 32) << 2)
       + ((n & 64) >> 6) + ((n & 128) >> 4) + ((n & 256) >> 2);
    

    In C#:

    public static int Count3x3() {
        HashSet<int> patterns = new HashSet<int>();
        for (int i = 0; i < 512; i++) {
            if ((i & 7) == 0 || (i & 73) == 0)
                continue;
            int nLowest = i;
            int n = i;
            do {
                nLowest = Math.Min(nLowest, n);
                n = ((n & 1) << 2) + ((n & 2) << 4) + ((n & 4) << 6)
                    + ((n & 8) >> 2) + (n & 16) + ((n & 32) << 2)
                    + ((n & 64) >> 6) + ((n & 128) >> 4) + ((n & 256) >> 2);
                while ((n & 73) == 0)
                    n >>= 1;
            } while (n != i);
            patterns.Add(nLowest);
        }
        return patterns.Count;
    }
    

    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:

    1. We can use a simple visited array instead of a hash set. If a pattern was seen before, we don’t count it. This also eliminates the need to keep track of the ‘lowest’ pattern in the inner loop. If a pattern was visited, then its lowest rotated pattern was visited, too.
    2. If we don’t have 180 degree rotation symmetry, then the 3rd rotation won’t yield the original pattern. The 4th rotation will, always, so it is unnecessary.
    3. The rotation expression can be slightly simplified.

    So, if we apply these observations and unroll the inner do loop, we get the following:

    static int Rotate(int n) {
        n = ((n & (1+32)) << 2) + ((n & 2) << 4) + ((n & 4) << 6)
            + ((n & (8+256)) >> 2) + (n & 16)
            + ((n & 64) >> 6) + ((n & 128) >> 4);
        while ((n & 73) == 0) 
            n >>= 1;
        return n;
    }
    public static int Count3x3_3() {
        bool[] visited = new bool[512];
        int count = 0, r;
        for (int i = 0; i < 512; i++) {
            if (visited[i])
                continue;
            if ((i & 7) == 0 || (i & 73) == 0)
                continue;
            count++;
            if ((r = Rotate(i)) == i) continue;
            visited[r] = true;
            if ((r = Rotate(r)) == i) continue;
            visited[r] = true;
            visited[Rotate(r)] = true;
        }
        return count;
    }
    

    This runs in about 3μs on the same machine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There's a movie which name I can't remember. It's about a carnival or amusement
I've been asked to create a hide/show function with a select tag. The function
I'm trying to load some XML into flash to populate some dynamic text fields.
I'm hoping to do something like this: data = read.table(mydata.txt, header=T)[,-1] test<-c(Piggies,Epstein,Majesty,Millionmiles,Mozambique,Deadpool,Blackbird,SamStonev2,Daytripper,Starr,Yellowsubmarine,Harrison,Taxman,Carey,Baez,Finn,Glassonionv2,Weight,Jericho,Monkey,Maggiemae,Cinnamongirl,Walrus,Anna,Handydandy,Pam,Wigwam,Psylocke,Coolwater,Wallflower,Cactustree,Hoist,Baronharkonnen,Coyote,BlueJay,NeilYoung,Harvest,KittyPryde,Beatles,Jam_session,Black_Crow,Carnival,Tombstone_blues,Sun_King,EmmaFrost,Sway,GhanimaAtreides,Heartland,Siona,Percy,Julia,Trouble,Sol_49,C21,Sol_18,i_126,i_114,Sol_6,Sinv_25,Sol_M3,Sol_20,Sol_42f,C316,C204,i_120_PigTail,C234,C368_PigTail,i_135,C1,C121_PigTail,C264,C367,Sinv12,C278_PT,i_125_PT,C485,Jerry_Garcia,C259,i_113,i_127,cassidy_PigTail,Sol_11,Jackstraw_PigTail,st_stephen,red_ant,sunrise,i_109,C216_pigtail,i129) fit1 <-
I'm building a Flash app (as3) that allows users to embed photos of themselves
I have a Java String which is actually an SQL script. CREATE OR REPLACE
Basically I need to get a list of CampaignTitles that have more than 2
i have a list listcdtitles = [ Liszt, Hungarian Rhapsody #6 {'Pesther Carneval'}; 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.