Skip to content

Instantly share code, notes, and snippets.

@sspencer
Created January 26, 2012 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sspencer/1681822 to your computer and use it in GitHub Desktop.
Save sspencer/1681822 to your computer and use it in GitHub Desktop.
Find Star of David 26 Solutions
// Given the Star of David, arrange the numbers 1-12 (there are 12 intersections) so that
// each line (there are 6 lines and 4 intersections per line) adds up to 26.
//
// Intersections on array are numbered TDLR, from a0 to a11.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// prototypes
int compute(int*);
void display(int *);
#define NVALUES 12
int compute(int *a)
{
return (a[0] + a[3] + a[6] + a[10] == 26) &&
(a[0] + a[2] + a[5] + a[7] == 26) &&
(a[1] + a[2] + a[3] + a[4] == 26) &&
(a[7] + a[8] + a[9] + a[10] == 26) &&
(a[1] + a[5] + a[8] + a[11] == 26) &&
(a[11] + a[9] + a[6] + a[4] == 26);
}
void display(int *a)
{
printf("[ ");
for (int i = 0; i < NVALUES; i++) {
printf("%d%c", a[i], (i < NVALUES-1 ? ',' : ' '));
}
printf("]\n");
}
int main(int argc, char *argv[])
{
// seed random numbers
srand48(time(0));
int a[NVALUES], i, count, solutions;
// initialize array with values 1-12
for(i = 0; i < NVALUES; i++) {
a[i] = i + 1;
}
count = 0;
solutions = 0;
while (solutions < 10) {
count++;
// shuffle
for(i = 0; i < NVALUES-1; i++) {
int r = (int)(drand48() * (NVALUES-i));
int t = a[i]; a[i] = a[i+r]; a[i+r] = t;
}
if (compute(a)) {
printf("Took %d iterations to find ", count);
display(a);
count = 0;
solutions++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment