Advertisement
CmdEngineer

Code Guru | Python Solution

Mar 28th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. ZERO_POS = ZERO_X, ZERO_Y = (1, 3)
  2. MIN, MAX = 0, 9
  3. MIN_X, MIN_Y = 0, 0
  4. MAX_X, MAX_Y = 2, 2
  5. EMPTY_DIR = None, None
  6.  
  7. # For testing...
  8. def main():
  9.     print(amount_call(int(input("Enter input: "))))
  10.  
  11. # Answer to question:
  12. def amount_call(length):
  13.     res = 0
  14.     for i in range(10):
  15.         res += amount_to_call_helper(length, *get_location(i))
  16.     return res
  17.  
  18. # Helper functions
  19. def amount_to_call_helper(length, x, y):
  20.     # Base for recursion:
  21.     if length == 1:
  22.         return 1
  23.     # Loop possible directions
  24.     res = 0
  25.     for dir_func in direction_functions:
  26.         if not dir_func(x, y) == EMPTY_DIR:
  27.             res += amount_to_call_helper(length - 1, *dir_func(x, y))
  28.     return res
  29.  
  30. # Parser function
  31. def get_location(cell):
  32.     # Zero is special:
  33.     if cell == 0:
  34.         return ZERO_POS
  35.     if MIN <= cell <= MAX:
  36.         cell_x = (cell - 1) % 3
  37.         cell_y = int((cell - 1) / 3)
  38.         return cell_x, cell_y
  39.     return (-1, -1)
  40.  
  41. # Location function
  42. def can_go_down_right(x, y):
  43.     if is_valid(x + 1, y + 2):
  44.         return x + 1, y + 2
  45.     return None, None
  46.  
  47. def can_go_down_left(x, y):
  48.     if is_valid(x - 1, y + 2):
  49.         return x - 1, y + 2
  50.     return None, None
  51.  
  52. def can_go_up_right(x, y):
  53.     if is_valid(x + 1, y - 2):
  54.         return x + 1, y - 2
  55.     return None, None
  56.  
  57. def can_go_up_left(x, y):
  58.     if is_valid(x - 1, y - 2):
  59.         return x - 1, y - 2
  60.     return None, None
  61.  
  62. def can_go_right_up(x, y):
  63.     if is_valid(x + 2, y - 1):
  64.         return x + 2, y - 1
  65.     return None, None
  66.  
  67. def can_go_right_down(x, y):
  68.     if is_valid(x + 2, y + 1):
  69.         return x + 2, y + 1
  70.     return None, None
  71.  
  72. def can_go_left_up(x, y):
  73.     if is_valid(x - 2, y - 1):
  74.         return x - 2, y - 1
  75.     return None, None
  76.  
  77. def can_go_left_down(x, y):
  78.     if is_valid(x - 2, y + 1):
  79.         return x - 2, y + 1
  80.     return None, None
  81.  
  82. def is_valid(x, y):
  83.     return MIN_X <= x <= MAX_X and MIN_Y <= y <= MAX_Y or (x, y) == ZERO_POS
  84.  
  85. # Assign all functions into a simple list
  86. direction_functions = [
  87.     can_go_down_left,
  88.     can_go_down_right,
  89.     can_go_left_down,
  90.     can_go_left_up,
  91.     can_go_right_down,
  92.     can_go_right_up,
  93.     can_go_up_left,
  94.     can_go_up_right
  95. ]
  96.  
  97. if __name__ == "__main__":
  98.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement