Skip to content

Instantly share code, notes, and snippets.

@andkon
Created December 4, 2017 06:46
Show Gist options
  • Save andkon/48b2e2c6e208db973cfa472e09a2f62f to your computer and use it in GitHub Desktop.
Save andkon/48b2e2c6e208db973cfa472e09a2f62f to your computer and use it in GitHub Desktop.
"""
Figure out how wide/tall the square is, and what the bottomright-most number is
Figure out how far from any corner your number is
Figure out how many layers deep the middle is
"""
import math
def third(number):
""" Returns the number of steps between number and 1 in that weird data table structure. """
sq = math.sqrt(number)
if sq > int(sq):
final_x = int(sq) + 2
else:
final_x = int(sq)
final_number = final_x**2
# now we can cheat a bit! forget calculating position, just figure out difference from the middle
x_remnant = ((final_number - number) % (final_x -1)) + 1 # tells us the position on the edge
x_movement = abs(((1+final_x)/2) - x_remnant) # diff between middle x and x_remnant
print(x_movement)
y_movement = ((1 + final_x)/2)-1 # tells us how far from the edge it is
print(y_movement)
return int(x_movement + y_movement)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment