Problem 184507 · hard · Phase 01 Prerequisites & Setup

When Does the Mower Get Here?

loops · simulation · coordinates

A robot mower starts on the square (0, 0) of an endless lawn and mows outwards in a square spiral. It moves one square per step, making straight runs of lengths 1, 1, 2, 2, 3, 3, 4, 4, ... and turning left (anticlockwise) after each run. The first run goes east (x grows), then north (y grows), then west, then south, then east again, and so on. So the first squares it visits are:

step 0: (0, 0)   step 1: (1, 0)   step 2: (1, 1)   step 3: (0, 1)   step 4: (-1, 1)
step 5: (-1, 0)  step 6: (-1, -1) step 7: (0, -1)  step 8: (1, -1)  step 9: (2, -1)

Write mow_step(x, y) that returns the step number at which the mower first stands on square (x, y).

Examples

Input:  x = 1, y = 1
Output: 2

Input:  x = 2, y = 0
Output: 10

Input:  x = -2, y = 2
Output: 16

Constraints

  • -3 * 10**4 <= x, y <= 3 * 10**4

Goals

  • Describe a spiral as straight segments with a simple length pattern
  • Test whether a point lies on a segment without walking it
  • Rotate a direction with two variables
Starting Python…