Skip to content

Instantly share code, notes, and snippets.

@marcoaaguiar
Last active July 26, 2016 05:43
Show Gist options
  • Save marcoaaguiar/8951fcab4b38918367b67cbe0bc926e6 to your computer and use it in GitHub Desktop.
Save marcoaaguiar/8951fcab4b38918367b67cbe0bc926e6 to your computer and use it in GitHub Desktop.
import numpy as np
# Initial Position
position = np.array([0,0])
# Target position
[A,B] = (10,10)
# Basic movements
UP_RIGHT = np.array([1,2])
UP_LEFT = np.array([-1,2])
RIGHT_UP = np.array([2,1])
RIGHT_DOWN = np.array([2,-1])
# Knights move in L shapes, but with 3 movements you can increase the position by 1 in one of the directions.
# To make position = position + (1,0)
def move_right(position):
position += -RIGHT_UP
position += UP_RIGHT
position += RIGHT_DOWN
# To make position = position + (0,1)
def move_up(position):
position += -UP_RIGHT
position += RIGHT_UP
position += UP_LEFT
# Did not used this, however this could be used to reduce the number of movements.
# To mke position = position + (1,1)
def move_diagonal(position):
position += UP_LEFT
position += RIGHT_DOWN
# This is the algorithm! (Sort of)
# I assumed that A and B are larger than 0
for i in range(A):
move_right(position)
for i in range(B):
move_up(position)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment