Skip to content

Instantly share code, notes, and snippets.

@harold
Created October 21, 2011 02:56
Show Gist options
  • Save harold/1303000 to your computer and use it in GitHub Desktop.
Save harold/1303000 to your computer and use it in GitHub Desktop.
Computers are fast
#include "stdafx.h"
#include "time.h"
#define d 5
int _tmain(int argc, _TCHAR* argv[])
{
time_t t = time(0);
int a[d*d], results[d*d+1];
for( int i=0; i<d*d; ++i ) a[i] = 0;
for( int i=0; i<d*d+1; ++i ) results[i] = 0;
for( int n=0; n<(1<<d*d); ++n )
{
// make a new a
for( int i=0; i<d*d; ++i )
a[i] = (n & (1<<i)) ? 1 : 0;
// count greens
int greens = 0;
for( int y=0; y<d; ++y )
{
for( int x=0; x<d; ++x )
{
int neighbors = 0;
if( x>0 && a[(x-1)+y*d]==1 ) neighbors++;
if( x<d-1 && a[(x+1)+y*d]==1 ) neighbors++;
if( y>0 && a[x+(y-1)*d]==1 ) neighbors++;
if( y<d-1 && a[x+(y+1)*d]==1 ) neighbors++;
if( neighbors%2==0 ) greens++;
}
}
results[greens]++;
}
// print results
for( int i=0; i<=d*d; ++i )
printf( "%d: %d\n", i, results[i] );
printf( "%d", time(0)-t );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment