Editor

Squares on the Corkboard

#378 · Geometry · Advanced · 135 XP · about 28 min

A corkboard has pins at the points in points, a list of different (x, y) tuples of integers. Write count_squares(points) that returns how many squares have a pin at each of their four corners, counting only squares whose sides are horizontal and vertical and longer than 0.

Two squares are different when their sets of corners are different. Pins inside a square or on its sides do not matter.

Examples

count_squares([(0, 0), (1, 0), (0, 1), (1, 1)])               # returns 1
count_squares([(x, y) for x in range(3) for y in range(3)])  # returns 5
count_squares([(0, 0), (2, 0), (1, 1), (1, -1)])              # returns 0

The 3 by 3 block of pins holds four 1 by 1 squares and one 2 by 2 square. The last four pins do form a square, but it is tilted, so it does not count.

Constraints

  • 0 to 2,000 points, all different.
  • Coordinates are between -106 and 106.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.