Skip to content

Instantly share code, notes, and snippets.

@jacoelho
Created December 4, 2017 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacoelho/e4edbb97ed9e81639b436a0dca492ff1 to your computer and use it in GitHub Desktop.
Save jacoelho/e4edbb97ed9e81639b436a0dca492ff1 to your computer and use it in GitHub Desktop.
advent of code 2017 day 3
import math
def coordinates(n):
d = math.floor(0.5 * (math.sqrt(n - 1) + 1))
nsig = n - (4 * d ** 2 + 1)
if nsig <= -2* d:
return(d, 3*d+nsig)
elif -2*d <= nsig <= 0:
return (-d - nsig, d)
elif 0 <= nsig <= 2*d:
return (-d, d-nsig)
elif 2*d <= nsig:
return (nsig -3*d, -d )
def distance(coord):
x, y = coord
return abs(x) + abs(y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment