<p>You are given <code>n</code> <code>points</code> in the plane that are all <strong>distinct</strong>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. A <strong>boomerang</strong> is a tuple of points <code>(i, j, k)</code> such that the distance between <code>i</code> and <code>j</code> equals the distance between <code>i</code> and <code>k</code> <strong>(the order of the tuple matters)</strong>.</p>

<p>Return <em>the number of boomerangs</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> points = [[0,0],[1,0],[2,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> points = [[1,1]]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == points.length</code></li>
	<li><code>1 &lt;= n &lt;= 500</code></li>
	<li><code>points[i].length == 2</code></li>
	<li><code>-10<sup>4</sup> &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>4</sup></code></li>
	<li>All the points are <strong>unique</strong>.</li>
</ul>

# 1 <= n <= 500

# the result is always a even number, since [a, b, c] will imply [a, c, b]
# we can always use distance**2 to reduce calculation

# brutal force, fix two points [b, c], and find a line to validate point a

# to reduce complexity to O(n**2), can pre-calculate distances for any pairs of points and insert into hash


class Solution:
    def calculate_distance(self, a: List[int], b: List[int]) -> int:
        return (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2

    def numberOfBoomerangs(self, points: List[List[int]]) -> int:
        if len(points) < 3:
            return 0

        dist_to_pairs: Dict[int, List[List[int]]] = {}
        for i in range(len(points) - 1):
            for j in range(i + 1, len(points)):
                dist = self.calculate_distance(points[i], points[j])
                if dist in dist_to_pairs:
                    dist_to_pairs[dist].append([i, j])
                else:
                    dist_to_pairs[dist] = [[i, j]]

        print(dist_to_pairs)
        count = 0
        for pairs in dist_to_pairs.values():
            for i in range(len(pairs) - 1):
                for j in range(i + 1, len(pairs)):
                    if pairs[i][0] == pairs[j][0] or \
                            pairs[i][0] == pairs[j][1] or \
                            pairs[i][1] == pairs[j][0] or \
                            pairs[i][1] == pairs[j][1]:
                        count += 1

        return count * 2
55700