Skip to content

Instantly share code, notes, and snippets.

@sspencer
Created January 26, 2012 07:43
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/1681566 to your computer and use it in GitHub Desktop.
Save sspencer/1681566 to your computer and use it in GitHub Desktop.
Make each line in star of david add up to 26
import random
import sys
"""
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.
"""
def compute(a):
return \
(a[0] + a[3] + a[6] + a[10] == 26) and \
(a[0] + a[2] + a[5] + a[7] == 26) and \
(a[1] + a[2] + a[3] + a[4] == 26) and \
(a[7] + a[8] + a[9] + a[10] == 26) and \
(a[1] + a[5] + a[8] + a[11] == 26) and \
(a[11] + a[9] + a[6] + a[4] == 26)
a = [1,2,3,4,5,6,7,8,9,10,11,12]
count = 0
solutions = 0
# find 10 solutions
while solutions < 10:
count += 1
random.shuffle(a)
if (compute(a)):
print "Took", count, " iterations to find", a
count = 0
solutions += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment