1

For about a year I've used this great short piece of code to spiral outwards. I now need a way to reverse this, so that I can put in "x:y" and get out the number which would return this.

https://stackoverflow.com/a/20591835/4333122

intRoot = int(sqrt(tileNum));

x = (round(intRoot / 2) * (-1 ^ (intRoot + 1))) + 
    ((-1 ^ (intRoot + 1)) * (((intRoot * (intRoot + 1)) - tileNum) -
    abs((intRoot * (intRoot + 1)) - tileNum)) / 2);

y = (round(intRoot / 2) * (-1 ^ intRoot)) + ((-1 ^ (intRoot + 1)) * 
    (((intRoot * (intRoot + 1)) - tileNum) + abs((intRoot * (intRoot + 1)) -
    tileNum)) / 2);

I can barely understand what I'm reading, so the best I can come up with is:

I see a pattern where there's 1 coordinate with just 0, 8 with 1 and 0, 16 with 1, 2 and 0, 24 with 3, 2, 1, 0, 32 with 4, 3, 2, 1, 0 etc - so whenever a new number is introduced to the spiral it has 8 more variations of coordinates than the previous number.

I could easily get the highest coordinate number in the input of, for example, "5:4" and know that there are (I think) 40 solutions, because 4 has 32, 3 has 24, 2 has 16 and 1 has 8.

I could then start at 32 and loop through all of the possible inputs for any number with a 5 in it and compare the output with my input, but if the number is high enough, there will be thousands to loop through.

Essentially, I have no clue. I tried to comment on the OP's submission but I don't have enough reputation.

Community
  • 1
  • 1
IAmATree
  • 11
  • 2

0 Answers0