Expand description
§Spiral Memory
§Part One
Consider the layout as a sequence of hollow donuts. We find the donut that contains the value which gives one component of the Manhattan value. The second component is the distance from the center of each edge.
For example say the target value is 20. We find the donut then subtract the inner donuts to make the values relative then calculate the values modulo the edge size.
<------------
17 16 15 14 13 7 6 5 4 3 3 2 [1] 0 3 ^
18 5 4 3 12 8 2 | 0 2 |
19 6 1 2 11 => 9 1 => | [1] [1] |
20 7 8 9 10 10 0 | 2 0 |
21 22 23 24 25 11 12 13 14 15 v 3 0 [1] 2 3
------------>
The first component is the horizonal or vertical distance from the centre to the ring, in this case 2 steps. The second component is the distance from the target number to the center of each edge, in this case 2 - 1 = 1.
§Part Two
We use the Point
utility to move in the spiral direction. Values are stored in a hashmap
defaulting to zero if the value doesn’t exist yet.