Created
December 25, 2010 06:23
-
-
Save jocull/754724 to your computer and use it in GitHub Desktop.
Fool's Spool - Brute Force Solution in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import copy | |
""" | |
Fool's spool number reels reference: | |
1 1 1 1 | |
5 2 5 4 | |
4 2 2 3 | |
2 3 4 2 | |
3 3 3 3 | |
4 5 1 5 | |
3 4 2 4 | |
5 3 3 3 | |
""" | |
#Reel sets | |
reelInitial = [[1,5,4,2,3,4,3,5], | |
[1,2,2,3,3,5,4,3], | |
[1,5,2,4,3,1,2,3], | |
[1,4,3,2,3,5,4,3]] | |
reel = copy.copy(reelInitial) | |
reelRowCount = len(reel) | |
reelRowLen = len(reel[0]) | |
reelRowGoal = 12 | |
haveSolution = False | |
attempts = 0 | |
while haveSolution != True: | |
print "Attempt #" + str(attempts) + " - " + str(reel) | |
#Check the current reel | |
solved = True | |
for i in range(0, reelRowLen): | |
sum = reel[0][i] + reel[1][i] + reel[2][i] + reel[3][i] | |
#print sum | |
if sum != reelRowGoal: | |
solved = False | |
break | |
if solved: | |
haveSolution = True | |
else: | |
attempts += 1 | |
#Spinning each row at random until we hit the solution | |
""" | |
for i in range(0, reelRowCount): | |
x = random.randrange(1, 8) | |
for y in range(0, x): | |
reel[i].append(reel[i].pop(0)) | |
""" | |
#Ordered spinning - more efficient, unless the the random script gets lucky | |
for i in range(0, reelRowCount): | |
if i == 0 or (attempts > 0 and attempts % ((i + 1) * reelRowLen) == 0): | |
reel[i].append(reel[i].pop(0)) | |
print "Solution found!" | |
print reel |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment