There is a special square room with mirrors on each of the four walls.  Except for the southwest corner, there are receptors on each of the remaining corners, numbered 0, 1, and 2.
The square room has walls of length p, and a laser ray from the southwest corner first meets the east wall at a distance q from the 0th receptor.
Return the number of the receptor that the ray meets first.  (It is guaranteed that the ray will meet a receptor eventually.)
 

Example 1:
Input: p = 2, q = 1
Output: 2
Explanation: The ray meets receptor 2 the first time it gets reflected back to the left wall.


Note:

1 <= p <= 1000
0 <= q <= p

Difficulty: MEDIUM_HARD
Tags: Geometry

Number theory

Mathematics

Starter code:
class Solution:
    def mirrorReflection(self, p: int, q: int) -> int:
        

Solutions:
class Solution:

	def mirrorReflection(self, p: int, q: int) -> int:
		while p % 2 == 0 and q % 2 == 0:
			p = p // 2
			q = q // 2
		if p % 2 == 1 and q % 2 == 0:
			return 0
		elif p % 2 == 1 and q % 2 == 1:
			return 1
		else:
			return 2


class Solution:

	def mirrorReflection(self, p: int, q: int) -> int:
		pos = 0
		wall = 0
		traj = 1
		while True:
			if wall == 0 and pos == p:
				return 2
			elif wall == 1 and pos == p:
				return 1
			elif wall == 1 and pos == 0:
				return 0
			if traj == 1:
				pos += q
				if pos > p:
					pos = 2 * p - pos
					traj *= -1
			else:
				pos -= q
				if pos < 0:
					pos *= -1
					traj *= -1
			wall = 1 - wall


class Solution:

	def mirrorReflection(self, p: int, q: int) -> int:
		(x, y) = (p, q)
		m = -q / p
		wall = 1
		while 1:
			b = y - m * x
			flag = 0
			if flag == 0 and wall != 0:
				temp = x
				x = -b / m
				if 0 <= x <= p:
					flag = 1
					y = 0
					wall = 0
				else:
					x = temp
			if flag == 0 and wall != 2:
				temp = x
				x = (p - b) / m
				if 0 <= x <= p:
					flag = 1
					y = p
					wall = 2
				else:
					x = temp
			if flag == 0 and wall != 3:
				temp = y
				y = b
				if 0 <= y <= p:
					flag = 1
					x = 0
					wall = 3
				else:
					y = temp
			if flag == 0 and wall != 1:
				temp = y
				y = m * p + b
				if 0 <= y <= p:
					flag = 1
					x = p
					wall = 1
				else:
					y = temp
			if (round(x, 2), round(y, 2)) == (p, 0):
				return 0
			if (round(x, 2), round(y, 2)) == (p, p):
				return 1
			if (round(x, 2), round(y, 2)) == (0, p):
				return 2
			m = -m


class Solution:

	def mirrorReflection(self, p: int, q: int) -> int:

		def gcd(p, q):
			while q > 0:
				(p, q) = (q, p % q)
			return p
		lcm = p * q // gcd(p, q)
		if lcm // q % 2 == 0:
			return 2
		elif lcm // p % 2 == 1:
			return 1
		else:
			return 0


class Solution:

	def mirrorReflection(self, p: int, q: int) -> int:
		hm = {0: [p, 0], 1: [p, p], 2: [0, p]}
		slope = (q - p) / p
		if slope == 0:
			return 1
		start = [0, p]
		while start:
			x2 = start[0] - start[1] / slope
			if x2 <= p:
				start = [x2, p]
				for i in hm:
					hm[i][1] = p - hm[i][1]
			else:
				y2 = start[1] + slope * (p - start[0])
				start = [0, y2]
				for i in hm:
					hm[i][0] = p - hm[i][0]
			for k in hm:
				if hm[k] == [round(start[0], 5), round(start[1], 5)]:
					return k


from fractions import Fraction as F

class Solution:

	def mirrorReflection(self, p: int, q: int) -> int:

		def line_y(x: int, x_0: int, y_0: int, d_x: int, d_y: int) -> int:
			return F(d_y, d_x) * (x - x_0) + y_0

		def line_x(y: int, x_0: int, y_0: int, d_x: int, d_y: int) -> int:
			return (y - y_0) / F(d_y, d_x) + x_0
		(x_0, y_0, d_x, d_y) = (0, 0, p, q)
		while True:
			if d_x > 0:
				y_intercept_right = line_y(p, x_0, y_0, d_x, d_y)
				if y_intercept_right == p:
					return 1
				elif y_intercept_right == 0:
					return 0
				elif 0 < y_intercept_right < p:
					(x_0, y_0, d_x, d_y) = (p, y_intercept_right, -d_x, d_y)
				elif y_intercept_right > p:
					(x_0, y_0, d_x, d_y) = (line_x(p, x_0, y_0, d_x, d_y), p, d_x, -d_y)
				else:
					(x_0, y_0, d_x, d_y) = (line_x(0, x_0, y_0, d_x, d_y), 0, d_x, -d_y)
			else:
				y_intercept_left = line_y(0, x_0, y_0, d_x, d_y)
				if y_intercept_left == p:
					return 2
				elif 0 < y_intercept_left < p:
					(x_0, y_0, d_x, d_y) = (0, y_intercept_left, -d_x, d_y)
				elif y_intercept_left > p:
					(x_0, y_0, d_x, d_y) = (line_x(p, x_0, y_0, d_x, d_y), p, d_x, -d_y)
				else:
					(x_0, y_0, d_x, d_y) = (line_x(0, x_0, y_0, d_x, d_y), 0, d_x, -d_y)


class Solution:

	def mirrorReflection(self, p: int, q: int) -> int:
		targets = [(p, 0), (p, p), (0, p)]
		from fractions import Fraction
		x = y = 0
		(rx, ry) = (p, q)
		while (x, y) not in targets:
			t = float('inf')
			for v in [Fraction(-x, rx), Fraction(-y, ry), Fraction(p - x, rx), Fraction(p - y, ry)]:
				if v > 0:
					t = min(t, v)
			(x, y) = (x + rx * t, y + ry * t)
			if x == p or x == 0:
				rx = -rx
			if y == p or y == 0:
				ry = -ry
		return targets.index((x, y))


class Solution:

	def mirrorReflection(self, p: int, q: int) -> int:
		from fractions import Fraction as F
		x = y = 0
		(rx, ry) = (p, q)
		targets = [(p, 0), (p, p), (0, p)]
		while (x, y) not in targets:
			t = float('inf')
			for v in [F(-x, rx), F(-y, ry), F(p - x, rx), F(p - y, ry)]:
				if v > 0:
					t = min(t, v)
			x += rx * t
			y += ry * t
			if x == p or x == 0:
				rx *= -1
			if y == p or y == 0:
				ry *= -1
		return 1 if x == y == p else 0 if x == p else 2


class Solution:

	def mirrorReflection(self, p: int, q: int) -> int:
		m = 1
		n = 1
		while m * p != n * q:
			n += 1
			m = n * q // p
		if n % 2 == 0:
			return 2
		if n % 2 == 1:
			if m % 2 == 1:
				return 1
			else:
				return 0


class Solution:

	def gcd(self, a: int, b: int) -> int:
		if b == 0:
			return a
		return self.gcd(b, a % b)

	def mirrorReflection(self, p: int, q: int) -> int:
		if q == 0:
			return 0
		div = gcd(p, q)
		(h, v) = (p // div, q // div)
		if not h % 2:
			return 2
		if v % 2:
			return 1
		return 0


class Solution:

	def mirrorReflection(self, p: int, q: int) -> int:
		from fractions import gcd
		g = gcd(p, q)
		p = p / g % 2
		q = q / g % 2
		if p and q:
			return 1
		elif p:
			return 0
		else:
			return 2


class Solution:

	def mirrorReflection(self, p: int, q: int) -> int:
		start = [0, 'left', 'up']
		if q == p:
			return 1
		if q == 0:
			return 0
		num = 0
		while True:
			num = num + 1
			print(start)
			tmp = []
			if start[2] == 'up':
				if start[0] + q <= p:
					if start[1] == 'left':
						tmp = [start[0] + q, 'right', 'up']
						if start[0] + q == p:
							return 1
					else:
						tmp = [start[0] + q, 'left', 'up']
						if start[0] + q == p:
							return 2
				elif start[1] == 'left':
					tmp = [2 * p - start[0] - q, 'right', 'down']
				else:
					tmp = [2 * p - start[0] - q, 'left', 'down']
			elif start[0] - q >= 0:
				if start[1] == 'left':
					tmp = [start[0] - q, 'right', 'down']
					if start[0] - q == 0:
						return 0
				else:
					tmp = [start[0] - q, 'left', 'down']
			elif start[1] == 'left':
				tmp = [q - start[0], 'right', 'up']
				if start[0] - q == 0:
					return 0
			else:
				tmp = [q - start[0], 'left', 'up']
			start = tmp


class Solution:

	def mirrorReflection(self, p: int, q: int) -> int:

		def determine_reflect_point(x1, y1, x2, y2):
			slope = (y2 - y1) / (x2 - x1)
			ref_slope = -slope
			if abs(x2 - p) < 10 ** (-2):
				val1 = [0, y2 + ref_slope * -p]
				val2 = [(0 - y2) / ref_slope + p, 0]
				val3 = [(p - y2) / ref_slope + p, p]
			if abs(x2) < 10 ** (-2):
				val1 = [p, y2 + ref_slope * p]
				val2 = [(0 - y2) / ref_slope + 0, 0]
				val3 = [(p - y2) / ref_slope + 0, p]
			if abs(y2) < 10 ** (-2):
				val1 = [p, ref_slope * (p - x2)]
				val2 = [0, ref_slope * (0 - x2)]
				val3 = [(p - 0) / ref_slope + x2, p]
			if abs(y2 - p) < 10 ** (-2):
				val1 = [p, p + ref_slope * (p - x2)]
				val2 = [0, p + ref_slope * (0 - x2)]
				val3 = [x2, p]
			if 0 <= val1[0] <= p and 0 <= val1[1] <= p:
				return (x2, y2, val1[0], val1[1])
			if 0 <= val2[0] <= p and 0 <= val2[1] <= p:
				return (x2, y2, val2[0], val2[1])
			if 0 <= val3[0] <= p and 0 <= val3[1] <= p:
				return (x2, y2, val3[0], val3[1])
		p1 = [0, 0]
		p2 = [p, q]
		while True:
			if abs(p2[0] - 0) < 10 ** (-2) and abs(p2[1] - p) < 10 ** (-2):
				return 2
			if abs(p2[0] - p) < 10 ** (-2) and p2[1] - 0 < 10 ** (-2):
				return 0
			if abs(p2[0] - p) < 10 ** (-2) and abs(p2[1] - p) < 10 ** (-2):
				return 1
			(p1[0], p1[1], p2[0], p2[1]) = determine_reflect_point(p1[0], p1[1], p2[0], p2[1])