#P12367. Minimum Steps on a Hexagonal Honeycomb
Minimum Steps on a Hexagonal Honeycomb
Minimum Steps on a Hexagonal Honeycomb
The honeycomb is formed by a tiling of regular hexagons. We define six directions on the honeycomb as follows:
- 0: West
- 1: West but 60° toward North
- 2: East but 60° toward North
- 3: East
- 4: East but 60° toward South
- 5: West but 60° toward South
For a given origin O, a point A is defined by taking a path starting at O that first moves p steps in direction d and then moves q steps in the direction (d+2) mod 6 (which is equivalent to a clockwise turn of 120° from d). The coordinate of point A is represented as (d, p, q). Note that a single point in the honeycomb might have multiple coordinate representations.
For example, the diagram shows the points B(0, 5, 3) and C(2, 3, 2).
Your task is: given two points represented as (d1, p1, q1) and (d2, p2, q2), determine the minimum number of steps required to travel from one point to the other on the honeycomb grid.
Note on Implementation: To solve this problem, one approach is to convert the given coordinate representation into axial coordinates. In our solution, we use the following mapping for the two basis directions:
- For a coordinate (d, p, q), let the first directional movement be along v[d] and the second one along v[(d+2) mod 6].
- We define the vectors in axial coordinates as follows:
v[0] = (-1, 0) (West)
v[1] = (-1, -1) (Northwest)
v[2] = (0, -1) (Northeast)
v[3] = (1, 0) (East)
v[4] = (1, 1) (Southeast)
v[5] = (0, 1) (Southwest)
The position of the point is computed as:
[ \text{position} = p \cdot v[d] + q \cdot v[(d+2) \bmod 6]. ]
The distance between two points in axial coordinates (x₁, y₁) and (x₂, y₂) can be calculated using:
[ \text{distance} = \frac{|x₁ - x₂| + |y₁ - y₂| + |(x₁+y₁) - (x₂+y₂)|}{2}. ]
</p>inputFormat
The input consists of two lines. The first line contains three integers: d1, p1, q1 representing the first point. The second line contains three integers: d2, p2, q2 representing the second point.
outputFormat
Output a single integer: the minimum number of steps required to travel from the first point to the second point.
sample
0 5 3
2 3 2
9