> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Mirror Reflection Python Solution with Tests

> Tested Python solution for LeetCode 858 with 21 pytest cases. Generate a practice environment with lcpy.

LeetCode 858, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [Geometry](/catalog/topics/geometry), [Number Theory](/catalog/topics/number-theory). [View on LeetCode](https://leetcode.com/problems/mirror-reflection/description/).

Generate this problem as a practice environment: tested reference solution, 21 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 858   # by problem number
lcpy gen -s mirror_reflection   # by problem name
```

## Problem

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 0^th receptor.

Given the two integers `p` and `q`, return the number of the receptor that the ray meets first.

The test cases are guaranteed so that the ray will meet a receptor eventually.

### Examples

![Example 1](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/18/reflection.png)

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

```
Input: p = 3, q = 1
Output: 1
```

### Constraints

* `1 <= q <= p <= 1000`

**Follow up:** Could you solve it without simulating the reflections?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/mirror_reflection/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/mirror_reflection/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from math import gcd


class Solution:
    # Time: O(log(min(p, q)))
    # Space: O(1)
    def mirror_reflection(self, p: int, q: int) -> int:
        g = gcd(p, q)
        heights, crossings = q // g, p // g
        if heights % 2 == 0:
            return 0
        return 2 if crossings % 2 == 0 else 1
```

## Complexity

| Time | Space |
| - | - |
| O(log(min(p, q))) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.