Last active
April 6, 2017 12:50
-
-
Save jakab922/fe30f37ab678d6daffbdf6a7705761a5 to your computer and use it in GitHub Desktop.
Towers of Hanoi move generator
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
def sol(n): | |
other = lambda x: 1 if x == 2 else 2 | |
first, empty = (1, 2) if n % 2 == 0 else (2, 1) | |
ms = [[1, 2, 0], [2, 0, 1]] | |
m = ms[empty % 2] | |
ret = [(0, first)] | |
for i in xrange(2, n + 1): | |
ret.append((0, empty)) | |
ret.extend(map(lambda x: (m[x[0]], m[x[1]]), ret[:-1])) | |
empty = other(empty) | |
m = ms[empty % 2] | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment