Skip to content

Instantly share code, notes, and snippets.

@yue82
Created December 30, 2016 13:55
Show Gist options
  • Select an option

  • Save yue82/a42b062a3a5f6e18d96f56cd0ad795ab to your computer and use it in GitHub Desktop.

Select an option

Save yue82/a42b062a3a5f6e18d96f56cd0ad795ab to your computer and use it in GitHub Desktop.
33c3 ctf hohoho's hanoi game solver
# -*- coding: utf-8 -*-
abc = ['1', '2', '3']
def move_pile(a, b, n, c):
if n == 0:
return ''
elif n == 1:
return (a + b) * 2
else:
tracks = move_pile(a, c, n - 1, b)
tracks += (a + b) * 2
tracks += move_pile(c, b, n - 1, a)
return tracks
def main(N):
a, b, c = abc
tracks = ''
for n in xrange(1, N+1):
tracks += a + b
tracks += move_pile(c, a, n - 1, b)
tracks += (b + c) * 2
tracks += move_pile(a, c, n - 1, b)
for n in xrange(N, 0, -1):
if n % 2 == 1:
aa, bb = a, b
else:
aa, bb = b, a
tracks += move_pile(c, aa, n - 1, bb)
tracks += (c + bb) * 2
tracks += move_pile(aa, c, n - 1, bb)
tracks += bb + aa
return tracks
if __name__ == '__main__':
print main(8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment