Skip to content

Instantly share code, notes, and snippets.

@jakab922
Last active April 6, 2017 12:50
Show Gist options
  • Save jakab922/fe30f37ab678d6daffbdf6a7705761a5 to your computer and use it in GitHub Desktop.
Save jakab922/fe30f37ab678d6daffbdf6a7705761a5 to your computer and use it in GitHub Desktop.
Towers of Hanoi move generator
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