Related:
Challenge:
Given a grid, with an ID starting at the center and spiraling out, what is the ID given a position in the fewest number of bytes?
Grid:
+---------------+---------------+---------------+---------------+---------------+
| id: 20 | id: 19 | id: 18 | id: 17 | id: 16 |
| pos: (-2, -2) | pos: (-1, -2) | pos: (0, -2) | pos: (1, -2) | pos: (2, -2) |
+---------------+---------------+---------------+---------------+---------------+
| id: 21 | id: 6 | id: 5 | id: 4 | id: 15 |
| pos: (-2, -1) | pos: (-1, -1) | pos: (0, -1) | pos: (1, -1) | pos: (2, -1) |
+---------------+---------------+---------------+---------------+---------------+
| id: 22 | id: 7 | id: 0 | id: 3 | id: 14 |
| pos: (-2, 0) | pos: (-1, 0) | pos: (0, 0) | pos: (1, 0) | pos: (2, 0) |
+---------------+---------------+---------------+---------------+---------------+
| id: 23 | id: 8 | id: 1 | id: 2 | id: 13 |
| pos: (-2, 1) | pos: (-1, 1) | pos: (0, 1) | pos: (1, 1) | pos: (2, 1) |
+---------------+---------------+---------------+---------------+---------------+
| id: 24 | id: 9 | id: 10 | id: 11 | id: 12 |
| pos: (-2, 2) | pos: (-1, 2) | pos: (0, 2) | pos: (1, 2) | pos: (2, 2) |
+---------------+---------------+---------------+---------------+---------------+
Tests:
f(0, 0) = 0
f(1, 1) = 2
f(0, -1) = 5
f(2, 0) = 14
f(-2, -2) = 20
f(x, y) = id
Notes:
- Grid will always be square (height == width)
- Grid can be infinite in size
java.awt.*
\$\endgroup\$20
, not24
\$\endgroup\$