> ## 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.

# Squirrel Simulation Python Solution with Tests

> Tested Python solution for LeetCode 573 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 573, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math). [View on LeetCode](https://leetcode.com/problems/squirrel-simulation/description/).

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

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

## Problem

You are given two integers `height` and `width` representing a garden of size `height x width`. You are also given:

* an array `tree` where `tree = [tree_r, tree_c]` is the position of the tree in the garden,
* an array `squirrel` where `squirrel = [squirrel_r, squirrel_c]` is the position of the squirrel in the garden,
* and an array `nuts` where `nuts[i] = [nut_i_r, nut_i_c]` is the position of the `i^th` nut in the garden.

The squirrel can only take at most one nut at one time and can move in four directions: up, down, left, and right, to the adjacent cell.

Return *the **minimal distance** for the squirrel to collect all the nuts and put them under the tree one by one*.

The **distance** is the number of moves.

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0573.Squirrel%20Simulation/images/squirrel1-grid.jpg)

```
Input: height = 5, width = 7, tree = [2,2], squirrel = [4,4], nuts = [[3,0], [2,5]]
Output: 12
```

**Explanation:** The squirrel should go to the nut at \[2, 5] first to achieve a minimal distance.

![Example 2](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0573.Squirrel%20Simulation/images/squirrel2-grid.jpg)

```
Input: height = 1, width = 3, tree = [0,1], squirrel = [0,0], nuts = [[0,2]]
Output: 3
```

### Constraints

* `1 <= height, width <= 100`
* `tree.length == 2`
* `squirrel.length == 2`
* `1 <= nuts.length <= 5000`
* `nuts[i].length == 2`
* `0 <= tree_r, squirrel_r, nut_i_r <= height`
* `0 <= tree_c, squirrel_c, nut_i_c <= width`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def min_distance(
        self, height: int, width: int, tree: list[int], squirrel: list[int], nuts: list[list[int]]
    ) -> int:
        tr, tc = tree
        sr, sc = squirrel
        to_tree = [abs(r - tr) + abs(c - tc) for r, c in nuts]
        total = 2 * sum(to_tree)
        return min(
            total - a + abs(r - sr) + abs(c - sc) for a, (r, c) in zip(to_tree, nuts, strict=True)
        )
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


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