Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ZERO_POS = ZERO_X, ZERO_Y = (1, 3)
- MIN, MAX = 0, 9
- MIN_X, MIN_Y = 0, 0
- MAX_X, MAX_Y = 2, 2
- EMPTY_DIR = None, None
- # For testing...
- def main():
- print(amount_call(int(input("Enter input: "))))
- # Answer to question:
- def amount_call(length):
- res = 0
- for i in range(10):
- res += amount_to_call_helper(length, *get_location(i))
- return res
- # Helper functions
- def amount_to_call_helper(length, x, y):
- # Base for recursion:
- if length == 1:
- return 1
- # Loop possible directions
- res = 0
- for dir_func in direction_functions:
- if not dir_func(x, y) == EMPTY_DIR:
- res += amount_to_call_helper(length - 1, *dir_func(x, y))
- return res
- # Parser function
- def get_location(cell):
- # Zero is special:
- if cell == 0:
- return ZERO_POS
- if MIN <= cell <= MAX:
- cell_x = (cell - 1) % 3
- cell_y = int((cell - 1) / 3)
- return cell_x, cell_y
- return (-1, -1)
- # Location function
- def can_go_down_right(x, y):
- if is_valid(x + 1, y + 2):
- return x + 1, y + 2
- return None, None
- def can_go_down_left(x, y):
- if is_valid(x - 1, y + 2):
- return x - 1, y + 2
- return None, None
- def can_go_up_right(x, y):
- if is_valid(x + 1, y - 2):
- return x + 1, y - 2
- return None, None
- def can_go_up_left(x, y):
- if is_valid(x - 1, y - 2):
- return x - 1, y - 2
- return None, None
- def can_go_right_up(x, y):
- if is_valid(x + 2, y - 1):
- return x + 2, y - 1
- return None, None
- def can_go_right_down(x, y):
- if is_valid(x + 2, y + 1):
- return x + 2, y + 1
- return None, None
- def can_go_left_up(x, y):
- if is_valid(x - 2, y - 1):
- return x - 2, y - 1
- return None, None
- def can_go_left_down(x, y):
- if is_valid(x - 2, y + 1):
- return x - 2, y + 1
- return None, None
- def is_valid(x, y):
- return MIN_X <= x <= MAX_X and MIN_Y <= y <= MAX_Y or (x, y) == ZERO_POS
- # Assign all functions into a simple list
- direction_functions = [
- can_go_down_left,
- can_go_down_right,
- can_go_left_down,
- can_go_left_up,
- can_go_right_down,
- can_go_right_up,
- can_go_up_left,
- can_go_up_right
- ]
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement