Skip to content

Instantly share code, notes, and snippets.

@veiset
Last active August 29, 2015 14:11
Show Gist options
  • Save veiset/69d66e8e804cdf4c5996 to your computer and use it in GitHub Desktop.
Save veiset/69d66e8e804cdf4c5996 to your computer and use it in GitHub Desktop.
MOVES = [[-2,-1],[-2,1],[-1,-2],[-1,2],[1,-2],[1,2],[2,-1],[2,1]]
KEYPAD = [[1, 2, 3],[4, 5, 6],[7, 8, 9],[None, 0, None]]
VALID = {(x, y) : key
for x,row in enumerate(KEYPAD)
for y,key in enumerate(row)
if not key == None}
GRAPH = {key : [VALID[(x+x2, y+y2)] for x2, y2 in MOVES if (x+x2, y+y2) in VALID]
for x, row in enumerate(KEYPAD)
for y, key in enumerate(row)
if not key == None}
def walk(graph, node, d=1, m=10):
return 1 if d == m else sum([walk(graph, n, d+1, m) for n in graph[node]])
print walk(GRAPH, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment