Skip to content

Instantly share code, notes, and snippets.

@KyleJohnstonNet
Created December 16, 2015 07:14
Show Gist options
  • Save KyleJohnstonNet/f21ee5a299cb135530cc to your computer and use it in GitHub Desktop.
Save KyleJohnstonNet/f21ee5a299cb135530cc to your computer and use it in GitHub Desktop.
#! /usr/bin/python
#Solves for the special case of hanoi ("Circular Hanoi"?) where you're movement
# is restricted along the directed graph:
# Start->Aux1->Aux2->...->Aux#->Dest->Start
pegs = ["Start", "Aux1", "Aux2", "Aux3", "Dest"]
n = 3
def f(pegs, n):
if n==1:
for i in range(0, len(pegs)-1):
print "Peg " + str(n) + " " + pegs[i] + "->" + pegs[i+1]
else:
f(pegs, n-1)
for i in range(0, len(pegs)-2):
print "Peg " + str(n) + " " + pegs[i] + "->" + pegs[i+1]
pegs2 = [pegs[i] for i in range(-1,len(pegs)-2)]
f(pegs2, n-1)
print "Peg " + str(n) + " " + pegs[-2] + "->" + pegs[-1]
pegs3 = [pegs[i] for i in range(-2,len(pegs))]
f(pegs3, n-1)
f(pegs, n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment