Created
December 4, 2017 20:03
-
-
Save jacoelho/e4edbb97ed9e81639b436a0dca492ff1 to your computer and use it in GitHub Desktop.
advent of code 2017 day 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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