Created
January 2, 2015 07:45
-
-
Save itarato/35d1fd2627177ec42e3f to your computer and use it in GitHub Desktop.
3 pegs
This file contains hidden or 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 sys | |
def solve(pegs, pfrom, pto, n): | |
if n == 1: | |
pegs[pfrom] -= 1 | |
pegs[pto] += 1 | |
print('{:d}->{:d}'.format(pfrom + 1, pto + 1)) | |
elif n > 1: | |
solve(pegs, pfrom, third_peg(pfrom, pto), n - 1) | |
solve(pegs, pfrom, pto, 1) | |
solve(pegs, third_peg(pfrom, pto), pto, n - 1) | |
def third_peg(peg_one, peg_two): | |
return 3 - peg_one - peg_two | |
if __name__ == '__main__': | |
print("3 pegs - " + sys.argv[1] + " cones") | |
arg_n = int(sys.argv[1]) | |
solve([arg_n, 0, 0], 0, 2, arg_n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment