Beehive
Time limit2sMemory limit512 MB
Given two cell indices in an infinite hexagonal beehive numbered by distance from cell 1, find the grid distance between those cells.
- Level
Medium5 of 10
- Topics
- Math, Geometry, Implementation
- Solved
- No attempts yet
Problem
An infinite beehive extends as in the figure. Two cells are adjacent exactly when they share a side. A path of length from cell to cell is a sequence of cells such that and are adjacent for every with . The distance between two cells is the length of a shortest path from one to the other.

Each cell of the beehive carries one positive integer index, as the figure shows. A cell farther from cell 1 gets a larger index, and among the cells at the same distance from cell 1 the index grows from left to right. Some cells at the same distance share a column. In the leftmost such column the lower cell comes first, and in the rightmost such column the upper cell comes first. The cells at one distance are therefore numbered by starting at the bottom left cell, climbing that column, crossing the top, and coming down to the bottom right cell. Every positive integer is the index of exactly one cell.
Given the indices of two cells, compute the distance between them.
Input
The input holds several test cases. Each test case is one line with two cell indices and separated by a single space (). The last line is 0 0, and that line is not a test case.
Output
For each test case, print the distance between the two given cells on its own line.