# Task
 In ChessLand there is a small but proud chess bishop with a recurring dream. 
 
 In the dream the bishop finds itself on an `n × m` chessboard with mirrors along each edge, and it is not a bishop but a ray of light. This ray of light moves only along diagonals (the bishop can't imagine any other types of moves even in its dreams), it never stops, and once it reaches an edge or a corner of the chessboard it reflects from it and moves on.

 Given the initial position and the direction of the ray, find its position after `k` steps where a step means either moving from one cell to the neighboring one or reflecting from a corner of the board.

# Example

 For `boardSize = [3, 7], initPosition = [1, 2], initDirection = [-1, 1] and k = 13,` the output should be `[0, 1]`.

 Here is the bishop's path:
```
[1, 2] -> [0, 3] -(reflection from the top edge)
         -> [0, 4] -> [1, 5] -> [2, 6] -(reflection from the bottom right corner)
         -> [2, 6] ->[1, 5] -> [0, 4] -(reflection from the top edge)
         -> [0, 3] ->[1, 2] -> [2, 1] -(reflection from the bottom edge)
         -> [2, 0] -(reflection from the left edge)
         -> [1, 0] -> [0, 1]```

 ![](https://codefightsuserpics.s3.amazonaws.com/tasks/chessBishopDream/img/example.png?_tm=1472324389202)

# Input/Output


 - `[input]` integer array `boardSize`

    An array of two integers, the number of `rows` and `columns`, respectively. Rows are numbered by integers from `0 to boardSize[0] - 1`, columns are numbered by integers from `0 to boardSize[1] - 1` (both inclusive).

    Constraints: `1 ≤ boardSize[i] ≤ 20.`


 - `[input]` integer array `initPosition`

    An array of two integers, indices of the `row` and the `column` where the bishop initially stands, respectively.

    Constraints:  `0 ≤ initPosition[i] < boardSize[i]`.


 - `[input]` integer array `initDirection`

    An array of two integers representing the initial direction of the bishop. 
    
    If it stands in `(a, b)`, the next cell he'll move to is `(a + initDirection[0], b + initDirection[1])` or whichever it'll reflect to in case it runs into a mirror immediately.

    Constraints: `initDirection[i] ∈ {-1, 1}`.


 - `[input]` integer `k`

    Constraints: `1 ≤ k ≤ 1000000000`.


 - `[output]` an integer array

    The position of the bishop after `k` steps.

Difficulty: EASY
Starter code: def chess_bishop_dream(board_size, init_position, init_direction, k):
	

Solutions:
def chess_bishop_dream(b, p, d, k):
	(yq, yr) = divmod(p[0] + k * d[0], 2 * b[0])
	(xq, xr) = divmod(p[1] + k * d[1], 2 * b[1])
	return [min(yr, 2 * b[0] - yr - 1), min(xr, 2 * b[1] - xr - 1)]


chess_bishop_dream = lambda b, p, d, k: [c - abs((q + k * e) % (2 * c) - c + 0.5) - 0.5 for (c, q, e) in zip(b, p, d)]


def chess_bishop_dream(board_size, init_position, init_direction, k):
	(l, (m, n), (x, y), (dx, dy)) = (0, board_size, init_position, init_direction)
	while k:
		k -= 1
		l += 1
		x += dx
		y += dy
		if x < 0 or x == m:
			dx = -dx
			x += dx
		if y < 0 or y == n:
			dy = -dy
			y += dy
		if [x, y] == init_position and [dx, dy] == init_direction:
			k %= l
	return [x, y]


def chess_bishop_dream(b, p, d, k):
	(yq, yr) = divmod(p[0] + k * d[0], b[0])
	(xq, xr) = divmod(p[1] + k * d[1], b[1])
	if yq < 0:
		yq = -yq
		yr = b[0] - yr - 1
	if xq < 0:
		xq = -xq
		xr = b[1] - xr - 1
	return [b[0] - yr - 1 if yq % 2 == (d[0] > 0) else yr, b[1] - xr - 1 if xq % 2 == (d[1] > 0) else xr]


def chess_bishop_dream(bs, init_pos, init_dir, k):
	(pos, dir) = (init_pos[:], init_dir[:])
	(n, start) = (k, (init_pos, init_dir))
	while n:
		pos = [x + dx for (x, dx) in zip(pos, dir)]
		for i in range(2):
			if not 0 <= pos[i] < bs[i]:
				dir[i] *= -1
				pos[i] = [0, bs[i] - 1][pos[i] > 0]
		n -= 1
		if start == (pos, dir):
			n %= k - n
	return pos


chess_bishop_dream = lambda b, p, d, k: [(lambda q, r: [r, c - r - 1][q % 2])(*divmod(q + k * e, c)) for (c, q, e) in zip(b, p, d)]


def chess_bishop_dream(board_size, init_position, init_direction, k):
	((h, w), (i, j), (di, dj)) = (board_size, init_position, init_direction)
	((qi, ri), (qj, rj)) = (divmod(i + di * k, h), divmod(j + dj * k, w))
	return [h - 1 - ri if qi % 2 else ri, w - 1 - rj if qj % 2 else rj]


def chess_bishop_dream(board_size, pos, dir, k):
	k %= board_size[0] * board_size[1] * 2
	steps = 0
	while steps < k:
		print(pos, steps, k, dir)
		rows_to_go = pos[0] if dir[0] == -1 else board_size[0] - pos[0] - 1
		cols_to_go = pos[1] if dir[1] == -1 else board_size[1] - pos[1] - 1
		n = min(rows_to_go, cols_to_go, k - steps)
		steps += n
		pos = [pos[0] + n * dir[0], pos[1] + n * dir[1]]
		if steps == k:
			break
		next_pos = [pos[0] + dir[0], pos[1] + dir[1]]
		valid_row = 0 <= next_pos[0] < board_size[0]
		valid_col = 0 <= next_pos[1] < board_size[1]
		if valid_col:
			dir[0] *= -1
			pos[1] = next_pos[1]
		elif valid_row:
			dir[1] *= -1
			pos[0] = next_pos[0]
		else:
			dir[0] *= -1
			dir[1] *= -1
		steps += 1
	return pos


def chess_bishop_dream(board_size, init_position, init_direction, k):
	(m, n) = board_size
	(x, y) = init_position
	(dx, dy) = init_direction
	r = [[x, y]]
	while True:
		(x, y) = (x + dx, y + dy)
		if not 0 <= x < m:
			x += -dx
			dx = -dx
		if not 0 <= y < n:
			y += -dy
			dy = -dy
		r.append([x, y])
		if len(r) > 2 and r[:2] == r[-2:]:
			break
	r = r[:-2]
	return r[k % len(r)]


def chess_bishop_dream(size, init_pos, init_dir, k):
	pos = init_pos.copy()
	dir = init_dir.copy()
	i = k
	while i > 0:
		check = [0 <= pos[0] + dir[0] < size[0], 0 <= pos[1] + dir[1] < size[1]]
		pos[0] += dir[0] * check[0]
		pos[1] += dir[1] * check[1]
		dir[0] *= int((int(check[0]) - 0.5) * 2)
		dir[1] *= int((int(check[1]) - 0.5) * 2)
		i -= 1
		if pos == init_pos and dir == init_dir:
			i %= k - i
	return pos