Skip to content

Instantly share code, notes, and snippets.

@skeeto

skeeto/keycount.c

Last active Nov 24, 2020
Embed
What would you like to do?
6x6 Hotel Key Card Counter
/* 6x6 Hotel Key Card Counter
* $ cc -fopenmp -O3 -march=native keycount.c
* Ref: https://possiblywrong.wordpress.com/2020/10/24/counting-hotel-key-cards/
*/
#include <stdio.h>
static long long
transpose(long long x)
{
return (x << 5 & 0x408102040) |
(x << 10 & 0x204081000) |
(x << 15 & 0x102040000) |
(x << 20 & 0x081000000) |
(x << 25 & 0x040000000) |
(x >> 0 & 0x810204081) |
(x >> 5 & 0x020408102) |
(x >> 10 & 0x000810204) |
(x >> 15 & 0x000020408) |
(x >> 20 & 0x000000810) |
(x >> 25 & 0x000000020);
}
static long long
flipv(long long x)
{
return (x >> 30 & 0x00000003f) |
(x >> 18 & 0x000000fc0) |
(x >> 6 & 0x00003f000) |
(x << 6 & 0x000fc0000) |
(x << 18 & 0x03f000000) |
(x << 30 & 0xfc0000000);
}
static int
valid(long long x)
{
int v = 0;
long long t;
t = transpose(x); v += t == x;
t = flipv(t); v += t == x;
t = transpose(t); v += t == x;
t = flipv(t); v += t == x;
t = transpose(t); v += t == x;
t = flipv(t); v += t == x;
t = transpose(t); v += t == x;
return !v;
}
int
main(void)
{
long long total = 0;
#pragma omp parallel for
for (int n = 0; n < 1<<8; n++) {
long long subtotal = 0;
for (long long i = 0; i < 1LL<<28; i++) {
subtotal += valid(i<<8 | n);
}
#pragma omp atomic
total += subtotal;
}
printf("%lld\n", total/8);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment