# Difference between revisions of "C code for Moser"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUM_LINES_3D 49

/* stat[i] is 0,1,2,3 depending on whether i (represented base 3) is of type a,b,c,d. */

const int stat[27] = {0,1,0, 1,2,1, 0,1,0,
1,2,1, 2,3,2, 1,2,1,
0,1,0, 1,2,1, 0,1,0};

int main()
{
/* There are NUM_LINES_3D lines in [3]^3.  Let's create their bitmasks...

 long line_bitmasks[NUM_LINES_3D];

 int i,j,k,count=0;
long i_bit, j_bit, k_bit;

  for (i=0, i_bit=1; i < 27; i++, i_bit *= 2)
for (j=0, j_bit=1; j < 27; j++, j_bit *= 2)
for (k=0, k_bit=1; k<27; k++, k_bit *= 2)
if (((i%3)+(k%3)-2*(j%3)) == 0)
if ((((i/3)%3)+((k/3)%3)-2*((j/3)%3))==0)
if ((((i/9)%3)+((k/9)%3)-2*((j/9)%3))==0)
if (i < k)
{
line_bitmasks[count] = i_bit + j_bit + k_bit;
count++;
}

/* There are 2^27 sets in [3]^3.   */
long NUM_SETS_3D = 1;
for (i=0; i<27; i++) NUM_SETS_3D *= 2;

  /* Now searching for line-free sets in [3]^3 by brute force. */
long set, num_linefree = 0, tmpset;
long numstat[9][13][7][2];  /* numstat[a][b][c][d] is the number of Moser sets with statistics (a,b,c,d) */
int a,b,c,d,s[4];

/* Initialise numstat */
for (a=0; a<9; a++)
for (b=0; b<13; b++)
for (c=0; c<7; c++)
for (d=0; d<2; d++)
numstat[a][b][c][d]=0;


   for (set=0; set<NUM_SETS_3D; set++)  /* Loop through all sets */
{
// it was marginally faster to put in these 49 lines in by hand rather than create a loop.

/* We have a line-free set! */

				/* now compute the a,b,c,d stats */
s[0]=s[1]=s[2]=s[3]=0;
tmpset = set;
for (j=0; j < 27; j++)
{
if (tmpset & 1l) s[stat[j]]++;
tmpset /= 2;
}
numstat[s[0]][s[1]][s[2]][s[3]]++;
num_linefree++;
}


count=0;
for (a=0; a<9; a++)
for (b=0; b<13; b++)
for (c=0; c<7; c++)
for (d=0; d<2; d++)
if (numstat[a][b][c][d] > 0)
{
count++;
printf("%ld sets with stats (%d,%d,%d,%d)\n",numstat[a][b][c][d],a,b,c,d);
}

printf("%ld line-free sets total, with %d different stats\n",num_linefree,count);
}